Wednesday 10 April 2013

Write a menu driven program using switch case statement (without functions) that has the following options: 1. Factorial of a number 2. Prime or not 3. Odd or even 4. Power ( x raise to power y) 5. Exit Input should be taken inside the particular case. For example ifthe user enters choice is 4. Then in case 4 your program can prompt the user to input value of x and then value of y. The switch case statement and the menu (mentioned above) should be in while(1) loop. The only way to end the program is that the user has to selection choice 5. In case 5 you can simply call a function exit (0);


#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
      int number,num1,sum,num2,i=1;
      while(i>0)
      {
      cout<<"\n1. FACTORIAL OF A NUMBER"<<endl;
      cout<<"2. NUMBER IS PRIME OR NOT"<<endl;
      cout<<"3. NUMBER IS EVEN OR ODD"<<endl;
      cout<<"4. POWER(x raise to power y)"<<endl;
      cout<<"5. EXIT"<<endl;
      cout<<"\n Enter Your Choice :"<<endl;
      cin>>number;
      switch(number)
      {
                    case 1:
                         {
                                 system("cls");
                                 cout<<"\nfactorial of a number"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 int f=1,i=1;
                                 while(i<=num1)
                                 {
                                 f=f*i;
                                 i++;    
                                 }
                                 cout<<"\n"<<num1<<"! = "<<f<<endl;
                                 break;
                                 }
                     case 2:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS PRIME OR NOT"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 int j=2;
                                 while(j<=num1-1)
                                 {
                                 if(num1%j==0)
                                 {
                                 cout << "\n" << num1 << " is not a prime number.";
                                 break;
                                 }
                                 i++;
                                 }
                                 if(j==num1)
                                 cout << "\n" << num1 << " is a prime number.";
                                 break;
                                 }
                     case 3:
                         {
                                 system("cls");
                                 cout<<"\nNUMBER IS EVEN OR ODD"<<endl;
                                 cout<<"\nEnter Number :"<<endl;
                                 cin>>num1;
                                 if(num1%2==0)
                                 {cout<<num1<<" is even number"<<endl;}
                                 else
                                 {cout<<num1<<" is odd number"<<endl;}
                                 break;
                                 }
                     case 4:
                         {
                                 system("cls");
                                 cout<<"\nPOWER(x raise to power y)"<<endl;
                                 cout<<"\nEnter value of x :"<<endl;
                                 cin>>num1;
                                 cout<<"Enter value of y :"<<endl;
                                 cin>>num2;
                                 sum=pow(num1,num2);
                                 cout<<"\n"<<num1<<" raise to power "<<num2<<" = "<<sum<<endl;
                                 break;
                                 }
                      case 5:
                         {
                                 exit(0);
                                 }
                                 i++;
      }
      }
getch();
}                  

No comments:

Post a Comment