bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_1 / assignment-primary / assignment-p-03.c

assignment-p-03.c (854B)


      1 /* Write a C program that includes a user-defined function named isPerfect with the signature 
      2 int isPerfect(int num);. A perfect number is a positive integer that is equal to the sum of 
      3 its proper divisors, excluding itself. For example, 28 is a perfect number because the sum 
      4 of its divisors (1, 2, 4, 7, 14) equals 28. */
      5 
      6 #include <stdio.h>
      7 
      8 int isPerfect(int);
      9 
     10 int main()
     11 {
     12     int n;
     13     printf("Enter the number: ");
     14     scanf("%d", &n);
     15     if (isPerfect(n))
     16     {
     17         printf("\nInput %d is a Perfect Number.", n);
     18     }
     19     else
     20     {
     21         printf("\nInput %d is not a Perfect Number.", n);
     22     }
     23     return 0;
     24 }
     25 
     26 int isPerfect(int n)
     27 {
     28     if (n <= 1)
     29         return 0;
     30     int temp = 1;
     31     int i;
     32     for (i = 2; i <= n / 2; i++)
     33     {
     34         if (n % i == 0)
     35         {
     36             temp += i;
     37         }
     38     }
     39     return temp == n;
     40 }