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