bsc

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

root / semester_1 / tuition-c / P038.c

P038.c (1318B)


      1 /* Write a program to check prime number */
      2 
      3 #include <stdio.h>
      4 #include <math.h>
      5 #include <stdbool.h>
      6 
      7 int main()
      8 {
      9     int num, i, endCheckDigit;
     10     bool isPrime = true;
     11     printf("Enter the number : ");
     12     if (scanf("%d", &num) != 1)
     13     {
     14         printf("\nOnly a number is allowed, not a character.");
     15         return 1;
     16     }
     17     if (num <= 0)
     18     {
     19         printf("\nOnly postive number is allowed.");
     20         return 1;
     21     }
     22     if (num == 1)
     23     {
     24         printf("\nInput 1 is not a prime number."
     25                "\nHas only one positive divisor (itself), not exactly two."
     26                "\nRule: Prime number should have exactly two distinct positive divisors: 1 and itself");
     27         return 0;
     28     }
     29     if (num == 2)
     30     {
     31         printf("\nInput 2 is a prime number."
     32                "\n(Note: 2 is only Even Prime Number)");
     33         return 0;
     34     }
     35     if (num % 2 == 0)
     36     {
     37         printf("\nInput %d is not a prime number.", num);
     38         return 0;
     39     }
     40     endCheckDigit = sqrt(num);
     41     for (i = 3; i <= endCheckDigit; i += 2)
     42     {
     43         if (num % i == 0)
     44         {
     45             isPrime = false;
     46             printf("\nInput %d is not prime number.", num);
     47             return 0;
     48         }
     49     }
     50     if (isPrime)
     51     {
     52         printf("\nInput %d is a prime number.", num);
     53     }
     54     return 0;
     55 }
© 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