# Perfect Number in C Programming Language

## Perfect Number

In mathematics, the perfect number is a positive integer that is the sum of its proper divisors, including one and excluding itself. For example, 6 is a perfect number. Divisors of 6(excluding 6) are 1,2,3, and their sum yields the number itself, i.e., 1+2+3=6.

Perfect numbers have fascinated mathematicians for centuries. Ancient Greeks were the first to identify perfect numbers and study them. The first few examples of perfect numbers are 6, 28, 496, and 8128. Perfect numbers are rare and have so many interesting properties. All the known perfect numbers are even.

## Finding the Perfect Number in C

We can find if the given number is perfect or not in programming. In programming, the way to check whether the given number is perfect is by using a loop and storing the sum of all the divisors in a variable. At last, we will check whether the sum of all the divisors except the number itself is equal to the number. If the number equals the sum, then the number is perfect. Otherwise, it is not. There are two approaches to achieving this. One uses a for loop, and the other uses a while loop.

### Using a For Loop

Here we will take an input and check whether it is a perfect number. First, let us form an algorithm.

Algorithm:

1. Declare the variables num, sum, i.
2. Take input from the user and store it in num.
3. Initialize sum to 0.
4. Make a loop from i=1 to i<(num-1).
5. Check if num%i is equal to 0.
6. If yes, then add it to the sum variable.
7. Exit the loop.
8. Check if sum is equal to num.
9. If yes, then print that number is perfect.
10. If no, then print that number is not perfect.
11. Exit

Now we will implement this algorithm in C language.

Code:

```#include <stdio.h>
int main()
{
int num, sum=0, i;
printf("Enter a number: ");
scanf("%d", &num);
for(i=1; i<num; i++)
{
if(num%i == 0)
{
sum += i;
}
}
if(sum == num)
{
printf("%d is a perfect number.", num);
}
else
{
printf("%d is not a perfect number.", num);
}
return 0;
}
```

This C program checks whether the number entered by the user is perfect. If it is a perfect number, then the program says the given number is perfect. If it is not a perfect number, it says that the given number is imperfect.

Output:

### Using While Loop

Now we will use another approach to check whether the given number is perfect. Let us first develop an algorithm for this approach.

Algorithm:

1. Declare the variables num, sum, i.
2. Take input from the user and store it in num.
3. Initialize the num variable to 0.
4. Initialize variable i to 1.
5. 5. Start a while loop with the condition i<num.
6. Check if num is divisible by i.
7. If yes, add i to the sum variable.
8. End if.
9. Increment i by 1.
10. End loop.
11. Check if the sum variable is equal to the num variable.
12. If yes, print that num is a perfect number.
13. If no, print that num is not a perfect number.
14. End if.
15. Exit

Now we will implement this algorithm in C programming language.

Code:

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

This program also performs the same task. The only difference is here; we are using a while loop instead of for loop.

Output:

We have both approaches to check whether a given number is perfect.

## Conclusion

Perfect numbers are an essential and fascinating concept of mathematics and computer science. Perfect numbers have interesting properties and have always been an area of research and study for mathematicians and computer scientists. Mathematicians and computer scientists can gain essential insights by understanding the basic concepts behind perfect numbers. All the perfect numbers are even.