Thursday, May 21, 2020

Switch statement in C++


  • Syntax :

     switch(expression
        {
     case x:
     // code block
     break;
     case y:
     // code block
     break;
     default:
     // code block
        }
  • Program :
#include <iostream>
using namespace std;
int main(){
   int a,b,n;
   cout <<"\n----------------SWITCH STATEMENT----------------\n";
   cout<<"\nThe given options are : \n1.Add \n2.sub \n3.multiply \n4.divide";
   cout<<"\nEnter your choice : ";
   cin>>n;
   cout<<"\nEnter A value : ";
   cin>>a;
   cout<<"\nEnter B value : ";
   cin>>b;
   switch(n) {
      case 1:
        cout<<"\n "<<a<<" + "<<b<<" = "<<a+b;
        break;
      case 2:
         cout<<"\n "<<a<<" - "<<b<<" = "<<a-b;
         break;
      case 3:
         cout<<"\n "<<a<<" * "<<b<<" = "<<a*b;
         break;
      case 4:
         cout<<"\n "<<a<<" / "<<b<<" = "<<a/b;
         break;
      default:
        cout<<"\nError";
        break;
   }
   return 0;
}

  • Output :

No comments:

Post a Comment

Function Overloading in C++

https://monitechvenkat1620.blogspot.com Program : #include <iostream> using namespace std; int c(int x, int y) {   return...