# C Program to Check for Perfect Numbers

Learn how to write a C program to check if a number is a perfect number. This guide includes a detailed explanation, algorithm, and complete code example for easy understanding.

A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. Proper divisors of a number are the positive divisors other than the number itself. For example, 28 is a perfect number because its proper divisors are 1, 2, 4, 7, and 14, and their sum is 1 + 2 + 4 + 7 + 14 = 28.

In this article, we will write a C program to check whether a given number is a perfect number or not. We will start by understanding the concept of perfect numbers, then outline the algorithm, and finally provide the complete C program with detailed explanations.

## Understanding Perfect Numbers

A perfect number is a number that satisfies the following condition:

Sum of proper divisors=Number

For example:

- 6 is a perfect number because its proper divisors are 1, 2, and 3, and their sum is 1 + 2 + 3 = 6.
- 28 is a perfect number because its proper divisors are 1, 2, 4, 7, and 14, and their sum is 1 + 2 + 4 + 7 + 14 = 28.

## Algorithm to Check for a Perfect Number

**Input the number**: Get the number from the user.**Find proper divisors**: Iterate through all numbers from 1 to the given number - 1 and find the divisors.**Sum the divisors**: Calculate the sum of all proper divisors.**Compare the sum with the original number**: If the sum is equal to the original number, it is a perfect number; otherwise, it is not.

## Write a C Program to Check for Perfect Numbers

Here is the complete C program to check whether a given number is a perfect number or not:

```
#include <stdio.h>
int main() {
int num, sum = 0;
// Input the number from the user
printf("Enter an integer: ");
scanf("%d", &num);
// Find and sum all proper divisors of the number
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
// Check if the sum of divisors is equal to the original number
if (sum == num && num != 0) {
printf("%d is a perfect number.", num);
} else {
printf("%d is not a perfect number.", num);
}
return 0;
}
```

**Output**

```
Enter an integer: 496
496 is a perfect number.
```

## Explanation of the Code

**Input the Number**:`scanf("%d", &num);`

reads the integer input from the user.

**Find and Sum Proper Divisors**:- The
`for`

loop iterates through all numbers from 1 to`num / 2`

to find the divisors. `if (num % i == 0)`

checks if`i`

is a divisor of`num`

.`sum += i;`

adds the divisor to the sum.

- The
**Check for Perfect Number**:- The condition
`if (sum == num && num != 0)`

checks if the sum of the proper divisors is equal to the original number. If true, it prints that the number is a perfect number; otherwise, it states that it is not.

- The condition

## Example Runs

Let's look at some example runs to see how the program works:

### Example 1

```
Enter an integer: 6
6 is a perfect number.
```

- Calculation: Proper divisors are 1, 2, 3. Sum = 1 + 2 + 3 = 6.

### Example 2

```
Enter an integer: 28
28 is a perfect number.
```

- Calculation: Proper divisors are 1, 2, 4, 7, 14. Sum = 1 + 2 + 4 + 7 + 14 = 28.

### Example 3

```
Enter an integer: 12
12 is not a perfect number.
```

- Calculation: Proper divisors are 1, 2, 3, 4, 6. Sum = 1 + 2 + 3 + 4 + 6 = 16.