C++ | Creating a simple calculator

Introduction

This post will create a simple calculator. 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

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int and char
int a,c,ans;
char b;
//Input the numbers and operators save it in variables.
cout<<"Enter the 1st no."<<endl;
cin>>a;
cout<<"Enter the 2nd no."<<endl;
cin>>c;
cout<<"Enter + for addition"<<endl;
cout<<"Enter - for substraction"<<endl;
cout<<"Enter x for multiplication"<<endl;
cout<<"Enter / for division"<<endl;
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;
//get character
getch();
}

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

Program to create a calculator

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int and char
int a,c,ans;
char b;
//Input the numbers and operators save it in variables.
cout<<"Enter the 1st no."<<endl;
cin>>a;
cout<<"Enter the 2nd no."<<endl;
cin>>c;
cout<<"Enter p for addition"<<endl;
cout<<"Enter m for substraction"<<endl;
cout<<"Enter x for multiplication"<<endl;
cout<<"Enter d for division"<<endl;
cin>>b;
//conditional switch statement.
switch (b)
{
case 'p':
ans=a+c;
break;
case 'm':
ans=a-c;
break;
case 'x':
ans=a*c;
break;
case 'd':
ans=a/c;
break;
default:
cout<<"Syntax Error";
}
//print the ans
cout<<"Ans = "<<ans;
//get character
getch();
}

Output

Enter the 1st no.
12
Enter the 2nd no.
5
Enter + for addition
Enter – for substraction
Enter x for multiplication
Enter / for division
p
Ans = 17

How does it work

  1. You enter 1st number
  2. You enter 2nd number
  3. You enter the operator.
  4. The conditional statement decides the operation and performs it.
  5. The ans is printed.

Extending it

The program can be extended by using more operators in the program. For example, you can add square and square root by using math.h and use trigonometrical operators. Go ahead and create a scientific calculator…..

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as int and char (name them as you want.)
  5. Add cout and cin of variable of input of numbers and operators.(as shown in program)
  6. Decide the operation by using switch conditional statement.
  7. Calculate the ans by respective operator.
  8. Print ans.

At the end

You learnt creating the c++ program of creating a simple calculator. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s