This post will create a simple calculator in C++. The first one use signs for deciding the operation and the next one uses characters for deciding the operation. You can further extend it and create a Scientific Calculator. Go enjoy the program. Lets begin……………
Program to create a Calculator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| #include<iostream> using namespace std; int main() { int a,c,ans; char b; //Input the numbers and operators save it in variables. cout<< "Enter the 1st no.\n" ; cin>>a; cout<< "Enter the 2nd no.\n" ; cin>>c; cout<< "Enter + for addition\n" ; cout<< "Enter - for substraction \n" ; cout<< "Enter * for multiplication \n" ; cout<< "Enter / for division \n" ; cin>>b; //conditional switch statement. switch (b) { case '+' : ans=a+c; break ; case '-' : ans=a-c; break ; case 'x' : ans=a*c; break ; case '/' : ans=a/c; break ; default : cout<< "Syntax Error" ; } //print the ans cout<< "Ans = " <<ans<<" \n";
system ("pause");
} |
Output
Enter the 1st no.
12
Enter the 2nd no.
5
Enter + for addition
Enter – for substraction
Enter x for multiplication
Enter / for division
x
Ans = 60
12
Enter the 2nd no.
5
Enter + for addition
Enter – for substraction
Enter x for multiplication
Enter / for division
x
Ans = 60