-->

How to Create a Simple Calculator Program in C Programming Language ?

Here we are writing a Calculator Program using the C Programming language.

This is one of the simplest program a beginner can write and understand a lot about computer programming.

The Calculator Program that we are writing here will work on two numbers and it will support Addition, Subtraction, Multiplication and Division operations.

Program Source Code : Calculator.c

 1 #include<stdio.h>
 2 
 3 int main(){
 4 
 5     char operator;
 6     double first, second;
 7 
 8     printf("Enter the Operator ( +, -, *, / ) : ");
 9     scanf("%c",&operator);
10 
11     printf("Enter the two Numbers one by one : ");
12     scanf("%lf %lf",&first,&second);
13 
14     switch (operator)
15     {
16 
17     case '+':
18         printf("%.2lf + %.2lf = %.2lf",first,second,(first+second));
19         break;
20         
21     case '-':
22         printf("%.2lf - %.2lf = %.2lf",first,second,(first-second));
23         break;
24 
25     case '*':
26         printf("%.2lf * %.2lf = %.2lf",first,second,(first*second));
27         break;
28 
29     case '/':
30         if( second != 0.0 )
31             printf("%.2lf / %.2lf = %.2lf",first,second,(first/second));
32         else 
33             printf("Divide by Zero situation");
34         break;
35     
36     default:
37         printf("%c is an invalid Operator",operator);
38         
39     }
40 
41     return 0;
42 }

Program Output : Run 1

Enter the Operator ( +, -, *, / ) : +
Enter the two Numbers one by one : 10 20
10.00 + 20.00 = 30.00

Program Output : Run 2

Enter the Operator ( +, -, *, / ) : -
Enter the two Numbers one by one : 10 30
10.00 - 30.00 = -20.00

Program Output : Run 3

Enter the Operator ( +, -, *, / ) : *
Enter the two Numbers one by one : 10 50
10.00 * 50.00 = 500.00

Program Output : Run 4

Enter the Operator ( +, -, *, / ) : /
Enter the two Numbers one by one : 10 20
10.00 / 20.00 = 0.50

Program Output : Run 5

Enter the Operator ( +, -, *, / ) : /
Enter the two Numbers one by one : 50 0
Divide by Zero situation

Program Output : Run 6

Enter the Operator ( +, -, *, / ) : ?
Enter the two Numbers one by one : 10 20
? is an invalid Operator

Video Tutorial : How to Write a Simple Calculator Program using C Programming language