Here's a c program to find the given no. is perfect no. or not with output and proper explanation.

What are Perfect Numbers?A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself e.g. 6 is a perfect number - Divisors of 6 are 1, 2, 3, 6 and 1 + 2 + 3 = 6. Another perfect number is 28.

# include <stdio.h> # include <conio.h> void main() { int i, n, s = 0 ; clrscr() ; printf("Enter a number : ") ; scanf("%d", &n) ; for(i = 1 ; i < n ; i++) { if(n % i == 0) s = s + i ; } if (s == n) printf("\n%d is a perfect number", n) ; else printf("\n%d is not a perfect number", n) ; getch() ; }

**Output of above program is**

Enter a number : 5

5 is not a perfect number

Enter a number : 6

6 is a perfect number

Enter a number : 28

28 is a perfect number

**Explanation of above program**

Here we have some integer variables let's understand the purpose of each first -

- n - This is the number that user enters.
- s - It is used to calculate the sum of all divisors of number n (excluding n itself). This sum is later compared with the original number to check for the perfect number.
- i - This is the loop variable.

First the user enters a number to check whether that number is perfect or not. Then the for loop will run for i = 1 to i < n or up to i = n - 1. Inside the loop we check whether the number n is completely divisible by every number from 1 to n - 1 by using this expression -

**n % i == 0.**If this expression is true then the if condition inside the loop gets executed and inside If we add that value of**i**to the sum variable**s**. Once the loop is finished we compare the sum variable and original number i.e.**n == s.**If this is true that means the number n is perfect otherwise it's not a perfect number.
Lets take an example - Suppose n = 6. The for loop will execute for i = 1 to i = 5. The process is as follows -

**i = 1;**Value of n % i = 6 % 1 == 0, so the if condition is executed and sum is updated as - s = s + i = 0 + 1 = 1.**i = 2;**Value of n % i = 6 % 2 == 0, so the if condition is executed and sum is updated as - s = s + i = 1 + 2 = 3.**i = 3;**Value of n % i = 6 % 3 == 0, so the if condition is executed and sum is updated as - s = s + i = 3 + 3 = 6.**i = 4;**Value of n % i = 6 % 4 == 2, The for loop will continue with the next value of i.**i = 5;**Value of n % i = 6 % 5 == 1, The for loop will terminate now.

Outside the loop n == s i.e. 6 == 6 is also true so the number 6 is perfect number.

Tip:To understand the program you can also take different values of n and try the above process yourself. It will help you to understand the process much clearly and helps in improving your debugging ability.

Hello There,

So bloody thorough! Ah! So happy and blessed out! I feel redeemed by reading out C Programming Tutorial Keep up the good work!

I tried to move my C code from SUN OS to AIX. It complied successfully on AIX machine where as it throws some memory related error message during run time.

It was cool to see your article pop up in my Google search for the process yesterday. Great Guide.

Keep up the good work!

Shukran,

Sindu