# C Program to Check whether a given Number is Perfect Number

«
»

This is a C Program to check whether a given number is perfect number.

Problem Description

This C Program checks whether a given number is perfect number.

Problem Solution

Perfect number is a number which is equal to sum of its divisor. For eg, divisors of 6 are 1,2 and 3. The sum of these divisors is 6. So 6 is called as a perfect number.

Program/Source Code

Here is source code of the C Program to check whether a given number is perfect number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C Program to Check whether a given Number is Perfect Number
*/
#include <stdio.h>

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

printf("Enter a Number\n");
scanf("%d", &number);
for (i = 1; i <= (number - 1); i++)
{
rem = number % i;
if (rem == 0)
{
sum = sum + i;
}
}
if (sum == number)
printf("Entered Number is perfect number");
else
printf("Entered Number is not a perfect number");
return 0;
}```
Program Explanation

In this C program, we are reading the integer value using ‘number’ variable. Perfect number is a number which is equal to sum of its divisor. For example, divisors of 6 are 1, 2 and 3. The sum of these divisors is 6. So the number 6 is called as perfect number.

For loop statement is used to assign the modulus of the value of ‘number’ variable by the value of ‘i’ variable. If condition statement is used to check the value of ‘rem’ variable is equal to 0, if the condition is true to execute if condition statement and compute the summation the value of ‘sum’ variable with the value of ‘i’ variable.

Another If-else condition statement is used to check that both the value of ‘sum’ variable and the value of ‘number’ variable are equal, if the condition is true print the statement as perfect number. Otherwise, execute else condition statement and print the statement as not a perfect number.

Runtime Test Cases
```
Output:
\$ cc pgm42.c
\$ a.out
Enter a Number
6
Entered Number is perfect number

\$ a.out
Enter a Number
100
Entered Number is not a perfect number```

Sanfoundry Global Education & Learning Series – 1000 C Programs. 