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 / practice-c / pc-ip-17.c

pc-ip-17.c (639B)


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