Turn Desktop View Off
by Dinesh Thakur Category: Control Structures

1. Factorial of a number.
2. Prime or not
3. Odd or even
4. Exit

Once a menu item is selected the appropriate action should be taken and once this action is finished, the menu should reappear. Unless the user selects the 'Exit' option the program should continue to work. 



#include<stdio.h>
#include<conio.h>
void main()
{
int choice, num, i;
unsigned long int fact;
clrscr();
while(1)
{
printf("1.Factorial\n");
printf("2.Prime\n");
printf("3.Odd/Even\n");
printf("4.Exit\n");
printf("\nYour choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter number:");
scanf("%d",&num);
fact = 1;
for(i=l;i<=num;i++)
fact=fact*i;
printf("\n Factorial value=%lu\n",fact);
break; // Takes control out of switch
case 2:
printf("\n Enter number:");
scanf("%d",&num);
for(i=2;i<num;i++)
{
if(num%i== 0)
{
printf("\n Not a prime number.\n");
break; //Takes control out of for loop
}
}
if(i==num)
printf("\n Prime number.\n");
break; //Takes control out of switch
case 3:
printf("\nEnter number:");
scanf("%d",&num);
if(num %2==0)
printf("\n Even number.\n");
else
printf("\nOdd number.\n");
break; //Takes control out of switch
case 4:
exit(); //Terminates program execution
}
}
getch();
}
 
OUTPUT:
 
1. Factorial
2. Prime
3. Odd/Even
4. Exit
Your choice ?1
Enter number: 5
factorial value=120
1. Factorial
2. Prime
3. Odd I Even
4. Exit
Your choice? 2
Enter number: 12
Not a prime number.
1. Factorial
2. Prime
3. Odd/Even
4. Exit
Your choice? 3
Enter number: 12
Even number.
1. Factorial
2. Prime
3. Odd/Even
4. Exit
Your choice? 4
exit
 
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article