Make Simple Calculator Program in C++

Google Advertisements

Prev Tutorial Next Tutorial

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!!

Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. For Advertisement contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs