lucproblem011.c (5594B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 12 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a menu driven program which has following options : 9 1. Factorial of a number 10 2. Prime or not 11 3. Odd or even 12 4. Exit 13 Once a menu item is selected the appropriate action should be taken 14 and once this action is finished, the menu should reappear. Unless 15 the user selects the 'Exit' option the program should continue work. 16 */ 17 /* Let Us C, Chap - 7, Page - 118, Problem 7.1 */ 18 19 #include <stdio.h> 20 #include <math.h> 21 #include <stdlib.h> 22 #include <stdbool.h> 23 24 // Function to clear the input buffer after scanf to prevent issues in the next input. 25 void clearInputBuffer() 26 { 27 int c; 28 while ((c = getchar()) != '\n' && c != EOF) 29 ; 30 } 31 32 // Calculates the factorial of the input number. 33 void factorial(int num) 34 { 35 // Factorial is not defined for negative numbers. 36 if (num < 0) 37 { 38 printf("\nFactorial is not defined for negative numbers."); 39 return; 40 } 41 // Checks for input over 20 to prevent long long integer overflow (20! is max safe). 42 if (num > 20) 43 { 44 printf("\nFactorial of %d is too large to calculate (max safe integer factorial is 20!).", num); 45 return; 46 } 47 long long fact = 1; 48 // Calculate factorial iteratively. 49 for (int i = 1; i <= num; i++) 50 { 51 fact *= i; 52 } 53 printf("\nFactorial of %d = %lld", num, fact); 54 return; 55 } 56 57 // Checks if the input number is a prime number. 58 void prime(int num) 59 { 60 // Handle special cases: 1 and 2. 61 if (num == 1) 62 { 63 printf("\nInput 1 is NOT a PRIME NUMBER."); 64 return; 65 } 66 else if (num == 2) 67 { 68 printf("\nInput 2 is a PRIME NUMBER. (Fact : 2 is only even prime number)"); 69 return; 70 } 71 // Exclude all other even numbers. 72 if (num % 2 == 0) 73 { 74 printf("\nInput %d is NOT a PRIME NUMBER.", num); 75 return; 76 } 77 // Optimization: Only check divisors up to the square root of num. 78 int endCheckDigit = sqrt(num); 79 bool isPrime = true; 80 // Check only odd divisors (i += 2) starting from 3. 81 for (int i = 3; i <= endCheckDigit; i += 2) 82 { 83 if (num % i == 0) 84 { 85 printf("\nInput %d is NOT a PRIME NUMBER.", num); 86 isPrime = false; 87 break; 88 } 89 } 90 if (isPrime) 91 { 92 printf("\nInput %d is a PRIME NUMBER.", num); 93 return; 94 } 95 } 96 97 // Checks if the input number is odd or even. 98 void oddoreven(int num) 99 { 100 // A number is even if it is perfectly divisible by 2. 101 if (num % 2 == 0) 102 { 103 printf("\nInput %d is a EVEN NUMBER.", num); 104 return; 105 } 106 else 107 { 108 printf("\nInput %d is a ODD NUMBER.", num); 109 return; 110 } 111 } 112 113 // Main function: displays the menu and controls program flow. 114 int main() 115 { 116 int choice, num; 117 // Infinite loop ensures the menu reappears after every operation until 'Exit' is chosen. 118 while (1) 119 { 120 // Display menu options. 121 printf("\n\n===== MENU =====" 122 "\n1. Factorial of a number" 123 "\n2. Prime or not" 124 "\n3. Odd or Even" 125 "\n4. Exit"); 126 printf("\nEnter your choice : "); 127 128 // Input validation for menu choice. 129 if (scanf("%d", &choice) != 1) 130 { 131 printf("\nPlease enter a number."); 132 clearInputBuffer(); 133 continue; 134 } 135 clearInputBuffer(); 136 137 // Handle menu selection using switch-case. 138 switch (choice) 139 { 140 case 1: 141 printf("\n=== FACTORIAL OF A NUMBER ==="); 142 printf("\nEnter the number : "); 143 // Input validation for the number to be factored. 144 if (scanf("%d", &num) != 1) 145 { 146 printf("\nPlease enter a number."); 147 clearInputBuffer(); 148 continue; 149 } 150 clearInputBuffer(); 151 factorial(num); 152 break; 153 case 2: 154 printf("\n=== PRIME OR NOT ==="); 155 printf("\nEnter the number : "); 156 // Input validation for the number to be checked. 157 if (scanf("%d", &num) != 1) 158 { 159 printf("\nPlease enter a number."); 160 clearInputBuffer(); 161 continue; 162 } 163 clearInputBuffer(); 164 // Require a non-negative, non-zero number for prime check. 165 if (num < 0) 166 { 167 printf("\nPlease enter a postive number."); 168 continue; 169 } 170 else if (num == 0) 171 { 172 printf("\nPlease enter a non-zero number."); 173 continue; 174 } 175 prime(num); 176 break; 177 case 3: 178 printf("\n=== ODD OR EVEN ==="); 179 printf("\nEnter the number : "); 180 // Input validation for the number to be checked. 181 if (scanf("%d", &num) != 1) 182 { 183 printf("\nPlease enter a number."); 184 clearInputBuffer(); 185 continue; 186 } 187 clearInputBuffer(); 188 oddoreven(num); 189 break; 190 case 4: 191 // Exit the program cleanly. 192 printf("\nExiting the program.\n\nSAYONARA...\n\n"); 193 exit(0); 194 default: 195 // Handle invalid menu choice input. 196 printf("\nPlease enter a valid choice."); 197 break; 198 } 199 } 200 }