Calculator-program-in-cpp-by-using-if-else-2022

Calculator program in cpp by using if else 2022


Calculator program in cpp

The Calculator Program is one of the most common programs in C/C++ for the basic practice of Arithmetic Operations, and other logical operations.

We can make the calculator program in our own term and with different control statements like here we are using if else statements in our calculator program.

Let’s create a simple calculator.

//.....Mymixindia.com...........
#include<iostream>
using namespace std;

int main()
{
 ///..... Calculator program in cpp by using if else 2022....
 cout<<"Calculator 3.0 \n";
 Menu:
    //....declaration of local variables, here a & b is for calculation and result for storing the result of calculation...
    int a,b,result;
    cout<<"Enter the first numbers for calculation \n";
    cin>>a;
    cout<<"Enter the second numbers for calculation \n";
    cin>>b;
cout<<"--------------------------------------\n";
 cout<<"Menu \n";
 cout<<"1 Add \n";
 cout<<"2 Subtract \n";
 cout<<"3 Multiply \n";
 cout<<"4 Divide \n";
 cout<<"5 Modulus \n";
 cout<<"Enter your choice \n";
 int choice; //...declaring a choice variable....
 cin>>choice; //...user input choice....

 if(choice == 1 )
 {
     cout<<"You select adition \n";
    result = a+b;
    cout<<"Your addtion result is result = "<<result;

 }
 else if(choice == 2){
     cout<<"You select Subtraction \n";
    result = a-b;
    cout<<"Your Substraction result is result = "<<result;
 }
 else if(choice == 3){
 
 cout<<"You select Multiplication \n";
 result = a*b;
    cout<<"Your Multiplication result is result = "<<result;
 
 } else if(choice == 4){
  
  cout<<"You select Division \n";
  result = a/b;
    cout<<"Your Division result is result = "<<result;
 
 } else if(choice == 5){
  
  cout<<"You select Modulus \n";
  result = a%b;
    cout<<"Your Modulus result is result = "<<result;
 }
 else{
 cout<<"Wrong Selection \n";
 }

 cout<<"\nDo you want to continue Y/N"<<endl;
 char ch = 'Y';
 cin>>ch;
 if(ch=='Y'||ch=='y')
 {
    goto Menu;
 }
 else{
     cout<<"Thankyou\n";
 }

 

    
    return 0;
}

The Output

Calculator 3.0 
Enter the first numbers for calculation 
10
Enter the second numbers for calculation 
20
--------------------------------------
Menu 
1 Add 
2 Subtract 
3 Multiply 
4 Divide 
5 Modulus 
Enter your choice 
1
You select adition 
Your addtion result is result = 30
Do you want to continue Y/N       
y
Enter the first numbers for calculation 
40
Enter the second numbers for calculation 
30
--------------------------------------
Menu 
1 Add 
2 Subtract 
3 Multiply
4 Divide
5 Modulus
Enter your choice
2
You select Subtraction 
Your Substraction result is result = 10
Do you want to continue Y/N
Y
Enter the first numbers for calculation 
12
Enter the second numbers for calculation 
24
--------------------------------------
Menu
1 Add
2 Subtract
3 Multiply
4 Divide
5 Modulus
Enter your choice
3
You select Multiplication 
Your Multiplication result is result = 288
Do you want to continue Y/N
Y
Enter the first numbers for calculation 
400
Enter the second numbers for calculation 
40
--------------------------------------
Menu
1 Add
2 Subtract
3 Multiply
4 Divide
5 Modulus
Enter your choice
4
You select Division 
Your Division result is result = 10
Do you want to continue Y/N
Y
Enter the first numbers for calculation 
365
Enter the second numbers for calculation 
100
--------------------------------------
Menu
1 Add
2 Subtract
3 Multiply
4 Divide
5 Modulus
Enter your choice
5
You select Modulus 
Your Modulus result is result = 65
Do you want to continue Y/N
Y
Enter the first numbers for calculation 
200
Enter the second numbers for calculation 
34
--------------------------------------
Menu
1 Add
2 Subtract
3 Multiply
4 Divide
5 Modulus
Enter your choice
9
Wrong Selection 

Do you want to continue Y/N
Y
Enter the first numbers for calculation 
20
Enter the second numbers for calculation 
30
--------------------------------------
Menu
1 Add
2 Subtract
3 Multiply
4 Divide
5 Modulus
Enter your choice
1
You select adition 
Your addtion result is result = 50
Do you want to continue Y/N
N
Thankyou

 

Explanation of the Program

The calculator program in c++ using if else having the calculator menu which displays the options of calculation and then the user has the choice of the input for add, subtract, multiplication, division, and modules. After the user inputs his/her choice then the program performs its operation according to the user choice. Like if he/she selected the option 1 add, then the program addition for the given input values and provide the result, the program asks for the continuation of calculation and asks for the user to enter the y or n for yes or no to continue by using goto statement.

This is the simple calculator program in c++ using if else.


Related Posts:

5 Namespace Program in C++ 2022

Constructors Program in Java

Java Data Type Program

HTML Tutorials 

CSS tutorials