C program for making a simple calculator for doing mathematical calculations




Calculator is a simple c program which is used for performing mathematical calculations such as addition, subtraction, multiplication and division. calculator program accepts two numbers from the user and also the mathematical operator for the calculation. after the operation we have to display the result.

For implementing a calculator program in c, we are using switch statement to do any of the operation based on the user input. Basic switch statement have cases to check which case user needs. Here we uses + , - , * , / ,also switch needs a default case which is taken if nothing else matches with the switch case.

===================================================================
#include <stdio.h>
 
#include <conio.h>
 
void main()
 
{
	char oper;			/* oper is an operator to be selected */
 
	float n1, n2, result;
 
	clrscr();
 
	printf ("Simulation of a Simple Calculator\n\n");    //print Simulation of a Simple Calculator, /n for next line
 
	printf("Enter two numbers\n");
 
	scanf ("%f %f", &n1, &n2);
 
	fflush (stdin);
 
	printf("Enter the operator [+,-,*,/]\n");
 
	scanf ("%c", &oper);
 
	switch (oper)
 
   	{
		case '+': result = n1 + n2;    // switch case 1 for addition
 
			  break;
 
		case '-': result = n1 - n2;    // case 2 for subtraction
 
			  break;
 
		case '*': result = n1 * n2;
 
			  break;
 
		case '/': result = n1 / n2;
 
			  break;
 
		default : printf ("Error in operation\n");  // default switch case error in operation
 
			  break;
	}
 
	printf ("\n%5.2f %c %5.2f= %5.2f\n", n1,oper, n2, result);  // prints the output of the c program
 
}
==================================================================
OUTPUT of c program for simple arithematic calculator
==================================================================
Simulation of Simple Calculator

Enter two numbers
3 5
Enter the operator [+,-,*,/]
+

3.00 + 5.00= 8.00


RUN2
Simulation of Simple Calculator

Enter two numbers
12.75
8.45
Enter the operator [+,-,*,/]
-

12.75 - 8.45= 4.30

RUN3
Simulation of Simple Calculator

Enter two numbers
12 12
Enter the operator [+,-,*,/]
*

12.00 * 12.00= 144.00


RUN4
Simulation of Simple Calculator

Enter two numbers
5
9
Enter the operator [+,-,*,/]
/

5.00 / 9.00= 0.56