Make Simple Calculator Program in C++
Advertisements
C++ Program to Make Simple Calculator
To understand below example, you have must knowledge of following C++ programming topics; Switch Case in C++ and Break Statement in C. In below code we use getch() function for get operator value from keyboard. If you have no idea about getch() function in C.
In below code we receive these arithmetic operator (+, -, *, /) and two operands from an user and perform operation on entered value by the users. After complete operation result will be display on screen.
C++ Program to Make Simple Calculator
#include<iostream.h> #include<conio.h> void main() { char choice; int a,b,res=0; clrscr(); cout<<"Enter First value: "; cin>>a; cout<<"\n Enter Operator: "; choice=getch(); cout<<choice; cout<<"\n Enter Second value: "; cin>>b; switch(choice) { case '+': res=a+b; cout<<"Sum: "<<res; break; case '-': res=a-b; cout<<"Difference: "<<res; break; case '*': res=a*b; cout<<"Product: "<<res; break; case '/': res=a/b; cout<<"Quotient: "<<res; break; default: cout<<"Enter Valid Operator!!"; } getch(); }
Output
Enter First number: 16 Enter operator : + Enter Second number: 20 Sum: 36
Output
Enter First number: 25 Enter operator : - Enter Second number: 12 Difference: 13
Output
Enter First number: 5 Enter operator : * Enter Second number: 12 Product: 60
Output
Enter First number: 26 Enter operator : / Enter Second number: 13 Quotient: 2
Output
Enter First number: 17 Enter operator : # Enter Second number: 3 Enter Valid Operator!!
Google Advertisment