C PROGRAM
Write a menu driven c program which perform following options:
- Factorial of a number
- Prime or not
- Odd or even
- 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 run.
Factorial, even or odd and prime or not in a c program | My CS Tutorial |
C PROGRAM SOURCE CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int c=0, num, res, n, flag=0, i;
clrscr();
while(c!=4)
{
//display menu
printf("\n 1. Factorial of a number\n 2. Prime or not\n 3. Odd or even\n 4. Exit\n");
//display choice option to the user
printf("\n Enter your choice:");
scanf("%d", &c);
//write case statement for Four options
switch(c)
{
//For factorial block
case 1:
//code for factorial functionality
printf("\n Enter an integer: ");
scanf("%d", &num);
n=num;
res=num;
while(num>1)
{
res = res*(num-1);
num = num-1;
}
printf("\n Factorial of %d is %d. \n\n",n, res);
break;
//For prime block
case 2:
//functionality of Prime or not
printf("\n Enter an integer: ");
scanf("%d", &num);
n=num;
for(i=2;i<=n/2;i++)
{
if(num%i==0)
{
flag=1;
break;
}
}
//for number "1" it's neither prime nor composite
if(num==1)
printf("\n 1 is neither prime nor composite");
else
{
if(flag==0)
printf("\n %d is Prime Number.\n\n", n);
else
printf("\n %d is not a Prime Number.\n\n", n);
}
break;
//For Odd-even block
case 3:
//functionality for Odd-even
printf("\n Enter an integer: ");
scanf("%d", &num);
n=num;
if(num%2==0)
printf("\n %d is Even Number.\n\n",n);
else
printf("\n %d is Odd Number.\n\n",n);
break;
//For Exit block
case 4:
printf("\n Exit");
break;
}
}
}
OUTPUT:-
1. Factorial of a number
2. Prime or not
3. Odd or even
4. Exit
Enter your choice:1
Enter an integer: 5
Factorial of 5 is 120.
1. Factorial of a number
2. Prime or not
3. Odd or even
4. Exit
Enter your choice:2
Enter an integer: 29
29 is Prime Number.
1. Factorial of a number
2. Prime or not
3. Odd or even
4. Exit
Enter your choice:3
Enter an integer: 53
53 is Odd Number.
1. Factorial of a number
2. Prime or not
3. Odd or even
4. Exit
Enter your choice:4
Exit
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment