Simple Calculator in c

Posted by admin at July 19, 2021

This program takes an arithmetic operator +, -, *, / and two operands from the user. Then, it performs the calculation on the two operands depending upon the operator entered by the user.


Simple Calculator using switch Statement

#include <stdio.h> 
int main() {
 char op; double first, second; 
 printf("Enter an operator (+, -, *, /): "); 
 scanf("%c", &op); 
 printf("Enter two operands: "); 
 scanf("%lf %lf", &first, &second); 
 switch (op) { 
    case '+': 
       printf("%.1lf + %.1lf = %.1lf", first, second, first + second); 
       break; 
    case '-': 
       printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
       break; 
    case '*': 
       printf("%.1lf * %.1lf = %.1lf", first, second, first * second); 
       break; 
    case '/': 
       printf("%.1lf / %.1lf = %.1lf", first, second, first / second); 
       break; 
    default: printf("Error! operator is not correct"); 
  } 
  return 0; 
}

Comments

Write a Reply or Comment