Write a menu driven program which has following option: 1. Factorial of a number 2. Prime or not 3. Odd or Even 4. Exit Make use of switch statement

Posted: September 23, 2012 in CSE Sem1, Lab 11B
#include<stdio.h>
int main()
{
	int choice;
	while(1)
	{
		printf("\n1. Factorial");
		printf("\n2. Prime");
		printf("\n3. odd/even");
		printf("\n1. your Choice?");
		scanf("%d",&choice);
		int i,n,fact = 1,a,c=2,num;
		switch(choice)
		{
			case 1:	
			 printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);
  for (i= 1; i <= n; i++)
    fact = fact * i;
  printf("Factorial of %d = %d\n", n, fact);
			break;
			case 2:
   printf("Enter a number to check if it is prime\n");
   scanf("%d",&a);
   for ( c = 2;c<= a-1;c++)
   {
      if ( a%c == 0 )
      {
         printf("%d is not prime.\n", a);
	 break;
      }
   }
   if ( c == a)
      printf("%d is prime.\n", a);
 
			break;
			case 3:
   printf("Enter an integer\n");
   scanf("%d",&num);
   if ( num%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");
			break;
		}

	}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s