Q:

Find Perfect Number using the function

belongs to collection: C Programming on Numbers

0

Find Perfect Number using the function

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
//return 1 is perfect number either return 0
int isPerfect(int num)
{
    int i,sum = 0;
    // Calculate sum of all proper divisors
    for(i = 1; i <= num / 2; i++)
    {
        //if i is a proper divisor of num
        if(num%i == 0)
        {
            sum += i;
        }
    }
    return (sum == num);
}
int main()
{
    int num;
    printf("Enter number = ");
    scanf("%d",&num);
    isPerfect(num)? printf("Perfect number"):printf("Not a perfect number");
    return 0;
}

 

Output:

Enter number = 6
Perfect number

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< C Program to Find the Roots of a Quadratic Equatio...