Make Simple Calculator Program in C++

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 Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements