**What is Perfect Number?**

A perfect number is the sum of all those numbers which are divisors of that number. For example, 6 is the perfect number. How? Divisors of 6 are 1, 2, 3, and 6. But we don’t have to consider 6 as a divisor here. Therefore, the sum of 1, 2 and 3 is 6. That is why we are saying 6 is the perfect number.

In this post, I am going to write a program to find a perfect number in c. I have written a total of 4 programs for the perfect number using for loop, while loop, do-while loop and function.

Before writing these all programs, let us see the expected output. Because it will help us to understand what we are going to do.

**Also Read: **Factorial Program in C Programming

**Expected Output**

First, we are going to ask the user to enter any number and then this program will give the result whether that number is a perfect number or not.

**Also Read: **C Program to Find the Sum of Cubes of Elements in an Array

**Perfect Number in C using for loop**

```
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2=0,i;
printf("Enter any number\n");
scanf("%d",&num1);
for(i=1;i<num1;i++)
{
if((num1%i)==0)
{
num2=num2+i;
}
}
if(num1==num2)
{
printf("%d is the Perfect Number",num1);
}
else
{
printf("%d is not the Perfect Number",num1);
}
return 0;
}
```

So, this is the program to find the perfect number using for loop. You can get the same output, using a while loop and do-while loop. Let’s see the other program.

**Also Read: **LCM of Two Numbers in C Programming

**Perfect Number in C using while loop**

```
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2=0,i;
printf("Enter any number\n");
scanf("%d",&num1);
i=1;
while(i<num1)
{
if((num1%i)==0)
{
num2=num2+i;
}
i++;
}
if(num1==num2)
{
printf("%d is the Perfect Number",num1);
}
else
{
printf("%d is not the Perfect Number",num1);
}
return 0;
}
```

**Perfect Number in C using do-while loop**

```
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2=0,i;
printf("Enter any number\n");
scanf("%d",&num1);
i=1;
do
{
if((num1%i)==0)
{
num2=num2+i;
}
i++;
}
while(i<num1);
if(num1==num2)
{
printf("%d is the Perfect Number",num1);
}
else
{
printf("%d is not the Perfect Number",num1);
}
return 0;
}
```

**Also Read: **C Program to Remove White Spaces and Comments from a File

Now, I am going to write the same program using function.

**Perfect Number in C using Function**

```
#include <stdio.h>
#include <stdlib.h>
int main()
{
int perfect(int);
int num1,num2=0,i;
printf("Enter any number\n");
scanf("%d",&num1);
num2=perfect(num1); // function call
if(num1==num2)
{
printf("%d is the Perfect Number",num1);
}
else
{
printf("%d is not the Perfect Number",num1);
}
return 0;
}
int perfect(int n)
{
int i,p=0;
for(i=1;i<n;i++)
{
if((n%i)==0)
{
p=p+i;
}
}
return p;
}
```

I hope, you understood all these programs. If you any doubt or feedback, then please feel free to contact me.

Thanks for reading.

**Important Programs**

- GCD of Two Numbers in C Programming
- Switch Case in C Programming
- Operators in C with Detailed Explanation
- Palindrome in C using Pointers
- Strong Number in C Programming
- Best 5 Programs on Fibonacci Series in C
- C Program to Display Middle Row and Column of Matrix
- Remove All Zeros from a number
- C Program to Print Multiples of 5 using do while loop
- Print Numbers Except Multiples of n