bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / lucproblem011.c

lucproblem011.c (5403B)


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