assignment-s-04.c (911B)
1 /* Write a function to check whether a number is prime or not. Use the same function to 2 generate all prime numbers less than 100. */ 3 4 #include <stdio.h> 5 #include <math.h> 6 7 int isPrime(int); 8 9 int main() 10 { 11 int n, i; 12 printf("Enter the number: "); 13 scanf("%d", &n); 14 if (isPrime(n)) 15 { 16 printf("\nInput %d is a Prime Number.", n); 17 } 18 else 19 { 20 printf("\nInput %d is not a Prime Number.", n); 21 } 22 printf("\nPrime Numbers less than 100:"); 23 for (i = 1; i < 100; i++) 24 { 25 if (isPrime(i)) 26 { 27 printf(" %d", i); 28 } 29 } 30 return 0; 31 } 32 33 int isPrime(int n) 34 { 35 if (n <= 1) 36 return 0; 37 if (n == 2) 38 return 1; 39 if (n % 2 == 0) 40 return 0; 41 42 int temp = (int)sqrt(n); 43 int i; 44 for (i = 3; i <= temp; i += 2) 45 { 46 if (n % i == 0) 47 { 48 return 0; 49 } 50 } 51 return 1; 52 }