C Online Compiler
Example: C Program to Make a Simple Calculator using If Else
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// C program to make a simple calculator using if else #include <stdio.h> #include <limits.h> int main() { char operator; double operand1 = 0, operand2 = 0; double result = 0; // INPUT the operator printf("Enter an operator from these - (+, -, *, /): "); scanf("%c", &operator); // INPUT the two values printf("\nEnter the two values to make calculation\n"); scanf("%lf %lf", &operand1, &operand2); if (operator== '+') { // Addition operation result = operand1 + operand2; } else if (operator== '-') { // Subtraction operation result = operand1 - operand2; } else if (operator== '*') { // Multiplication operation result = operand1 * operand2; } else if (operator== '/') { // Division operation // Check for division by zero if (operand2 != 0) { result = operand1 / operand2; } else { printf("\nError! Division by zero.\n"); result = INT_MIN; } } else { printf("\nError! Operator is not correct.\n"); result = INT_MIN; } printf("\nResult: %.2lf\n", result); return 0; }
+ 90 35
Output
Clear
ADVERTISEMENTS