bsc

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

root / semester_1 / practice-c / pc-ip-04.c

pc-ip-04.c (875B)


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