-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu driven program
90 lines (78 loc) · 2.23 KB
/
Menu driven program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Menu driven program
#include<stdio.h>
#include<stdlib.h>
int main()
{
int choice,num,i,fact;
while(1)
{
//Display the menu options
printf("\n1.Factorial\n");
printf("2.Prime\n");
printf("3.Odd/Even\n");
printf("4.Exit\n");
printf("Your Choice? ");
//Read the user's choice
scanf("%d",&choice);
//Validate the user's choice
if(choice < 1 || choice > 4)
{
printf("Wrong choice!\a\n");
continue; //go back to the beginning of the loop
}
//Exit the program if the user chooses 4
if(choice == 4)
{
exit(0);
}
//Ask the user to enter a number
printf("\nEnter number: ");
scanf("%d",&num);
//Validate the user's number
if(num < 0)
{
printf("Invalid number!\n");
continue; //go back to the beginning of the loop
}
//Perform the corresponding operation based on the user's choice
switch(choice)
{
case 1: //Factorial
fact = 1;
for(i = 1; i <= num; i++)
{
fact = fact * i;
}
printf("Factorial value = %d\n",fact);
break;
case 2: //Prime
for(i = 2; i < num; i++)
{
if(num % i == 0)
{
printf("Not a prime number\n");
break;
}
}
if(i == num)
{
printf("Prime number\n");
}
break;
case 3: //Odd/Even
if(num % 2 == 0)
{
printf("Even number\n");
}
else
{
printf("Odd number\n");
}
break;
default:
//This should never happen
printf("Something went wrong!\n");
}
}
return 0;
}