Please disable your adblock and script blockers to view this page
 

Else if ladder to perform operation on two numbers based on operator in C++ language

Introduction:  In previous article i explained  Else if ladder or multiple else if to check the day corresponding to number in C++ Language and Else if ladder or multiple else if to check the month corresponding to number in C++ Language. In this article i  am going to explain how to perform addition(+), subtraction(-), multiplication(*), division(/) and modulus (%) operations on two numbers corresponding to selected operator from + , - , * , / ,%  using ladder else if or we can say multiple else if statement in C++ Language.Modulus operator(%) gives the remainder after integer division e.g divide 10 by 4 and you will get 2 as remainder Let's understand by creating a program.

#include<conio.h>
#include<iostream.h>
main()
{
      int a,b,c;
       char op;
       cout<<"Enter first number : ";
       cin>>a;
       cout<<"Enter second number : ";
       cin>>b;
      cout<<"Enter any  operator from + , - , * , / ,% : ";
      cin>>op;
                      if(op=='+')
                            {
                             c=a+b;   
                            cout<<"Sum of "<<a<<" and  "<<b<<"  =  "<<c;
                            }
                      else if(op=='-')
                            {
                             c=a-b;    
                            cout<<"Subtraction of "<<a<<" and  "<<b<<"  =  "<<c;
                            }
                     
                      else if(op=='*')
                            {
                             c=a*b;   
                            cout<<"Multiplication of "<<a<<"  and  "<<b<<"  =  "<<c;
                            }
   
                     else if(op=='/')
                            {
                             c=a/b;   
                            cout<<a<<" divided by  "<<b<<"  =  "<<c;
                            }                                
                    else if(op=='%')
                            {
                             c=a%b;   
                            cout<<"Remainder of "<<a<<" divided by  "<<b<<"  =  "<<c;
                            }
                    else
                           {
                            cout<<"INVALID OPERATOR!!  Please enter correct one";
                           }
                    getch();
                    }
Run the program using Ctrl+F9
                   
Output:
First Run
Enter first number :  4
Enter second number : 2
Enter  any operator from + , - , * , / ,% : *

Multiplication of 4 and 2 = 8

Second Run
Enter first number :  10
Enter second number : 40
Enter  operator from + , - , * , / ,% : +

Addition of 10 and 40 = 50

Third Run
Enter first number :  100
Enter second number : 20
Enter  operator from + , - , * , / ,% :  -
Subtraction  of 100 and 20 = 80

Fourth Run
Enter first number :  40
Enter second number : 5
Enter  operator from + , - , * , / ,% :  /

40 divided by 5= 8

Fifth Run
Enter first number :  20
Enter second number :4
Enter  operator from + , - , * , / ,% :  %

Remainder of 20 divided by 4 = 0                   
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon