C++ Program Using Switch Statement

Switch case is used if there is multiple choices and  each case is created for each choice and operation is completed separately

#include<iostream>
using namespace std;
int main()
{


int n,i,a,b,A,S,M,c,d,fac=1;
float D;
cout <<"Press 1 for Addition of Numbers\n";
cout <<"Press 2 for Subtraction of Numbers\n";
cout <<"Press 3 for Multiplication of Numbers\n";
cout <<"Press 4 for Division of Numbers\n";
cout <<"Press 5 for Factortial of Number\n";
cout <<"Press 6 for Fibonacci Series\n";
cout <<"Press 7 for Exit\n";
cin >> n;

switch(n)
{
case 1:
cout << " Enter any two numbers";
cin >> a;
cin >> b;
A=a+b;
cout << " Addition of given numbers is "<<A;
break;
case 2:
cout << " Enter any two numbers";
cin >> a;
cin >> b;
S=a-b;
cout << " Subtraction of given numbers is "<<S;
break;
case 3:
cout << " Enter any two numbers";
cin >> a;
cin >> b;
M=a*b;
cout << " Multiplication of given numbers is "<<M;
break;
case 4:
cout << " Enter any two numbers";
cin >> a;
cin >> b;
D=a/b;
cout << " Division of given numbers is "<<D;
case 5:
cout << "Enter any number";
cin >> n;
for (i=1;i<=n;i++)
{
fac=fac*i;
}

cout << " Factorial of the given number is"<< fac;
break;
case 6:
b=0;c=1;
cout <<"  "<< b ;
cout << " " << c;
for (i=0;i<10;i++)
{

d=b+c;
cout << " " <<d;
b=c;
c=d;

}
break;
case 7:
return 0;
break;
}
return 0;
}


Comments

  1. You should add a comment, it is somewhat difficult to understand for a newbie like me. But I like the way you do. It is neat and Clean :)

    ReplyDelete

Post a Comment