If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Here is an example of a simple calculator program written in C language:
Copy code
#include <stdio.h>
int main() {
double num1, num2;
char operator;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
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;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
This program prompts the user to enter an operator (one of +, -, *, or /) and two operands. It then performs the specified operation and prints the result to the screen. The program uses a switch statement to determine which operation to perform based on the operator entered by the user.
Comments: 0