C Program - Simple Calculator
Simple Calculator
This blog post will describe a simple calculator program written in
C.
Program Objective
The objective of this program is to perform simple arithmetic operations
(addition, subtraction, multiplication, and division) on two numbers input
by the user. The program takes two numbers as input, an operator (+, -, *,
or /), and outputs the result of the corresponding operation.
Pseudocode for This Program
1. Declare variables for two numbers (num1, num2) and an operator (operator)
2. Prompt the user to enter two numbers and store them in the variables num1 and num2
3. Prompt the user to enter an operator (+, -, *, or /) and store it in the variable operator
4. Perform a switch statement based on the value of the operator variable:
a. For operator "+", print the result of num1 + num2
b. For operator "-", print the result of num1 - num2
c. For operator "*", print the result of num1 * num2
d. For operator "/", print the result of num1 / num2
e. For all other values of operator, print an error message "Invalid operator"
5. End the program
Source Code for This Program
int main() {
double num1, num2;
char operator;
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", num1, num2, num1 + num2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", num1, num2, num1 - num2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", num1, num2, num1 * num2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", num1, num2, num1 / num2);
break;
default:
printf("Error: Invalid operator");
}
return 0;
}
Compile The Source Code
To compile this program, you can use the following steps:
- Open a terminal or command prompt.
- Navigate to the directory where the program file is stored using the cd command.
- Compile the program using the following command:
$ gcc filename.c -o executable_name
You do not need to make the file executable explicitly. The gcc command automatically creates an executable file when you compile the program. The executable file is created with the name you specified using the -o option in the gcc command.
After you have compiled the program using the gcc command, you can run the program by typing ./executable_name, where executable_name is the name you specified in the gcc command. This makes the operating system treat the file as an executable program, allowing you to run it.
Run the Executable File
Run the program by typing the following command:
$ ./executable_name
Console Output
Enter two numbers: 2.5 7.5
Enter an operator (+, -, *, /): +
2.5 + 7.5 = 10.0
This example shows a scenario where the user inputs 2.5 and 7.5 as the two numbers and + as the operator. The program then performs the addition operation and outputs the result 10.0.
Note that the output may vary depending on the inputs and the specific implementation of the program.
Image by 200 Degrees from Pixabay
Comments
Post a Comment