# Perfect Number Program in C

In this post, we will write the Perfect number program in C. We will also write a program to check the perfect number in a given range.

Perfect number:- A number whose factors sum, except itself equals the same number.

Example:- 6
Factors of 6 (except itself) are 1,2,3.
The Sum of these factors 1+2+3 = 6 So, 6 is a Perfect number.

Another example:- 28
Factors of 28 (except itself) are 1, 2, 4, 7, 14
The sum of factors of 28 = 1+2+4+7+14 = 28
Hence, 28 is also a perfect number.

The perfect number program is based on finding the factors of a number. If you don’t know how to find factors of a number using the C program then learn it first.

## C program to Check Perfect Number

`````` #include<stdio.h>
int main()
{
int num, sum=0;

printf("Enter Number: ");
scanf("%d",&num);

for(int i=1; i<=num/2; i++)
{
if(num%i==0)
sum+=i;
}

if( sum==num )
printf("%d is a perfect number.\n",num);
else
printf("%d is not a perfect number.\n",num);

return 0;
}``````

Output:-

Enter Number: 6
6 is a perfect number.

Enter Number: 10
10 is not a perfect number.

Enter Number: 28
28 is a perfect number.

The same problem also can be done using while loop.

## C program to check Perfect Number in a given Range

`````` #include<stdio.h>
int main()
{
int min, max, sum;

printf("Enter min and max value of the range: ");
scanf("%d %d", &min, &max);

printf("Perfect numbers in the given range are: \n");

for(int i=min; i<=max; i++)
{
sum=0;
for(int j=1; j<=i/2; j++)
{
if(i%j==0)
sum+=j;
}
if(i==sum) printf("%d\t",i);
}

return 0;
}``````

Output:-

Enter min and max value of the range:` 1 100`
Perfect numbers in the given range are:
6 28

Enter min and max value of the range: `100 10000`
Perfect numbers in the given range are:
496 8128

## C program to find the perfect number using the Function

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming.

The function is a small program is used to do a particular task. In C a big program divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

### Function to check perfect number

`````` int checkPerfect(int number)
{
int sum = 0;
for(int i=1;i<=number/2;i++)
{
if(number%i==0)
sum += i;
}
if( sum == number ) return 0;
else return 1;
}``````

Below program checks, the given number is a perfect number or not.

`````` #include<stdio.h>
int checkPerfect(int number)
{
int sum = 0;

for(int i=1;i<=number/2;i++)
{
if(number%i==0)
sum += i;
}

if( sum == number ) return 0;

else return 1;
}

int main()
{
int number, sum=0;

printf("Enter Number: ");
scanf("%d",&number);

if(checkPerfect(number) == 0)
printf("%d is a perfect number.\n",number);
else
printf("%d is not a perfect number.\n",number);

return 0;
}``````

Output:-

Enter Number: 496
496 is a perfect number.

Enter Number: 500
500 is not a perfect number.

Using the `checkPerfect()` function we can also write a C program to find perfect number in a given range, try it yourself.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!