Thursday, September 4, 2014

Write a menu driven program using switch case statement which calculates factorial of a number, prime or not, even or odd, and power


Problem

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 if the 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);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int c,n,b,i,j,g;
 printf("Select one :\n 1. factorial.\n2. prime nmber:\n3. even or odd:\n4. exit.\n Enter choice:");
 scanf("%d",&c);
 switch(c)
 {
  case 1:
   printf("enter no.:");
   scanf("%d",&n);
   b=1;
   for(i=n;i>0;i--)
    b=b*i;
   printf("factorial is %d",b);
   system("pause");
   break;
  case 2 :
   printf("Enter no.:");
   scanf("%d",&j);
   if(j%2==0)
   {
    printf("it is not a prime numbr.\n");
   }
   else
    printf("it is a prime number.\n");
   system("pause");
   break;
  case 3 :
   printf("enter the :");
   scanf("%d",&g);
   if(g%2==0)
    printf("it is an even number.\n");
   else
    printf("not an even number\n");
   system("pause");
   break;
  case 4 :
   exit(0);
 }
 system("pause");
}

Sample Output


2 comments :

  1. For prime number the program is wrong

    ReplyDelete
    Replies
    1. Thanks For Your Kind Response. Will Definitely Look Into It ASAP.

      Delete

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact