Calculator using If Statement in C++

Simple calculator using if-else statement in C++ programming. In this tutorials you will learn how to write a simple calculator which allows you to add, subtract, and multiply integers values.

Program using AND Operator in If Statement

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"Enter value from 1 to 5: ";
cin>>num; //user enter value storing in 'num'.
if(num >= 1 && num <= 5) //if 'num' value Less then or equal to 1 And
{                          //greater then OR equal to 5 then
cout<<"Welcome!"; //print this, and U also write like
}                    //'if((num >= 1) && (num <= 5))'.
else //if not then
{
cout<<"Invalid Entry!"; //print this.
}
getch();
}
Output
Enter value from 1 to 5: 4
Welcome!

Program using OR Operator in If Statement

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"Enter value from 1 to 4: ";
cin>>num; //user enter value storing in 'num'.
if(num == 1 || num == 2 || num == 3 || num == 4) //if 'num' value equal to 1
{ //OR equal to 2 OR equal to 3 OR equal to 4 then
cout<<"Welcome!"; //print this.
}
else //if not then
{
cout<<"Invalid Entry!"; //print this.
}
getch();
}
Output
Enter value from 1 to 4: 2
Welcome!

Program Calculator in If Statement

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a, b;
char op;
cout<<"Enter First Integer: ";
cin>>a;
cout<<"Enter Operator: ";
cin>>op; //user enter operator storing in 'op'.
cout<<"Enter 2nd Integer: ";
cin>>b;
if(op == '+') //if 'op' value equal to '+' then
{
cout<<"Addition is: "<<a + b; //print this.
}
else if(op == '-') //if 'op' value equal to '-' then
{
cout<<"Subtraction is: "<<a - b; //print this.
}
else if(op == '*') //if 'op' value equal to '*' then
{
cout<<"Multiplication is: "<<a * b; //print this.
}
else //if not in above then
{
cout<<"Invalid Operator"; //print this.
}
getch();
}
//Always use Single Quote '''' for Character, and for Integer dont use any Quote.
Output
Enter First Integer: 32
Enter Operator: +
Enter 2nd Integer: 13
Addition is: 45