The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Perfect Number

What is a Perfect number?

A perfect number is defined to be one which is equal to the sum of all its divisible number. For example let us consider a number 6, the numbers which are all divisible by 6 are 1, 2, 3. Then 1 + 2 + 3 = 6 . Thus 6 is a perfect number.

perfect number

Perfect Number - Examples

Some of the following perfect numbers are

Number Explanation
6 1 + 2 + 3
28 1 + 2 + 4 + 7 + 14
496. 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
8128 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064

C Program - To Find a Perfect Number

Let us write a c program to find whether a user entered number is a perfect number or not.

c-perfect-number.c
#include <stdio.h>
int main()
{
int a, b, num;
printf("Enter a number : ");
scanf("%d",&num);
a = 1;
b = 0;
while(num > a)
{
if(num % a == 0)
b = b + a;
a++;
}
if(b == num)
printf("\n%d is a perfect number", a);
else
printf("\n%d is not a perfect number", a);
return 0;
}
Enter a number : 28
28 is a perfect number

Note:

while loop is used for iterating a value from 1 to one number less than user entered number. If the numbers from 1 is divisible by user entered number, then the numbers are added in a dummy variable b. finally we check whether b == user entered number. If so we will declare as a perfect number else it will be declared as not a perfect number.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report