lucproblem011.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 12 Dec 2025
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Write a menu driven program which has following options : 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 work.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
// Function to clear the input buffer after scanf to prevent issues in the next input.
void clearInputBuffer()
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
;
}
// Calculates the factorial of the input number.
void factorial(int num)
{
// Factorial is not defined for negative numbers.
if (num < 0)
{
printf("\nFactorial is not defined for negative numbers.");
return;
}
// Checks for input over 20 to prevent long long integer overflow (20! is max safe).
if (num > 20)
{
printf("\nFactorial of %d is too large to calculate (max safe integer factorial is 20!).", num);
return;
}
long long fact = 1;
// Calculate factorial iteratively.
for (int i = 1; i <= num; i++)
{
fact *= i;
}
printf("\nFactorial of %d = %lld", num, fact);
return;
}
// Checks if the input number is a prime number.
void prime(int num)
{
// Handle special cases: 1 and 2.
if (num == 1)
{
printf("\nInput 1 is NOT a PRIME NUMBER.");
return;
}
else if (num == 2)
{
printf("\nInput 2 is a PRIME NUMBER. (Fact : 2 is only even prime number)");
return;
}
// Exclude all other even numbers.
if (num % 2 == 0)
{
printf("\nInput %d is NOT a PRIME NUMBER.", num);
return;
}
// Optimization: Only check divisors up to the square root of num.
int endCheckDigit = sqrt(num);
bool isPrime = true;
// Check only odd divisors (i += 2) starting from 3.
for (int i = 3; i <= endCheckDigit; i += 2)
{
if (num % i == 0)
{
printf("\nInput %d is NOT a PRIME NUMBER.", num);
isPrime = false;
break;
}
}
if (isPrime)
{
printf("\nInput %d is a PRIME NUMBER.", num);
return;
}
}
// Checks if the input number is odd or even.
void oddoreven(int num)
{
// A number is even if it is perfectly divisible by 2.
if (num % 2 == 0)
{
printf("\nInput %d is a EVEN NUMBER.", num);
return;
}
else
{
printf("\nInput %d is a ODD NUMBER.", num);
return;
}
}
// Main function: displays the menu and controls program flow.
int main()
{
int choice, num;
// Infinite loop ensures the menu reappears after every operation until 'Exit' is chosen.
while (1)
{
// Display menu options.
printf("\n\n===== MENU ====="
"\n1. Factorial of a number"
"\n2. Prime or not"
"\n3. Odd or Even"
"\n4. Exit");
printf("\nEnter your choice : ");
// Input validation for menu choice.
if (scanf("%d", &choice) != 1)
{
printf("\nPlease enter a number.");
clearInputBuffer();
continue;
}
clearInputBuffer();
// Handle menu selection using switch-case.
switch (choice)
{
case 1:
printf("\n=== FACTORIAL OF A NUMBER ===");
printf("\nEnter the number : ");
// Input validation for the number to be factored.
if (scanf("%d", &num) != 1)
{
printf("\nPlease enter a number.");
clearInputBuffer();
continue;
}
clearInputBuffer();
factorial(num);
break;
case 2:
printf("\n=== PRIME OR NOT ===");
printf("\nEnter the number : ");
// Input validation for the number to be checked.
if (scanf("%d", &num) != 1)
{
printf("\nPlease enter a number.");
clearInputBuffer();
continue;
}
clearInputBuffer();
// Require a non-negative, non-zero number for prime check.
if (num < 0)
{
printf("\nPlease enter a postive number.");
continue;
}
else if (num == 0)
{
printf("\nPlease enter a non-zero number.");
continue;
}
prime(num);
break;
case 3:
printf("\n=== ODD OR EVEN ===");
printf("\nEnter the number : ");
// Input validation for the number to be checked.
if (scanf("%d", &num) != 1)
{
printf("\nPlease enter a number.");
clearInputBuffer();
continue;
}
clearInputBuffer();
oddoreven(num);
break;
case 4:
// Exit the program cleanly.
printf("\nExiting the program.\n\nSAYONARA...\n\n");
exit(0);
default:
// Handle invalid menu choice input.
printf("\nPlease enter a valid choice.");
break;
}
}
}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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182