bsc

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

root / semester_1 / assignment-primary / assignment-p-01.c

assignment-p-01.c (837B)


      1 /* Write a C program that includes a user-defined function named isPrime with the signature 
      2 int isPrime(int num); The function should take an integer as a parameter and return 1 if 
      3 the number is prime and 0 otherwise. */
      4 
      5 #include <stdio.h>
      6 #include <math.h>
      7 
      8 int isPrime(int);
      9 
     10 int main()
     11 {
     12     int n;
     13     printf("Enter the number: ");
     14     scanf("%d", &n);
     15 
     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 
     25     return 0;
     26 }
     27 
     28 int isPrime(int n)
     29 {
     30     if (n <= 1)
     31         return 0;
     32     if (n == 2)
     33         return 1;
     34     if (n % 2 == 0)
     35         return 0;
     36 
     37     int temp = (int)sqrt(n);
     38     int i;
     39     for (i = 3; i <= temp; i += 2)
     40     {
     41         if (n % i == 0)
     42         {
     43             return 0;
     44         }
     45     }
     46     return 1;
     47 }
© 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