Before going to the program to find Perfect Number or Not first let us understand what is a Perfect Number?

Perfect Number:

                A Perfect Number is a number that is equal the sum of its positive divisors excluding the given Number.

For example,

                6 is a Perfect Number, because 6 is equal to the sum of its positive divisors (ie. 1 + 2 + 3 = 6).

Program code for Perfect Number or Not in C:

#include<stdio.h>
#include<conio.h> void main()
{ int n, i=1, sum=0; clrscr(); printf("n Enter a number: "); scanf("%d", &n); /* Loop to calculate the sum of positive divisors */ while(i<n) { if(n%i==0) { sum=sum+i; } i++; } /* if-else condition to print Perfect Number or Not */ if(sum==n) printf("n %d is a Perfect Number.",n); else printf("n %d is Not a Perfect Number.",n); getch();
}

Related: C program to find Perfect Number or Not using For Loop

Working:

  • First the computer reads the positive integer value from the user.
  • Then using while loop it calculates the sum of positive divisors.
  • Finally if-else condition is used to print the number is perfect number or not .

Step by Step working of the above Program Code:

For Perfect Number:

  1. Let us assume that a user enters the positive integer value as 6.
  2. It assigns the value of sum=0 and n=6.
  3. It assigns the value of i=1 and the loop continues till the condition of the while loop is true.

3.1.   i<n    (1<6)    while loop condition is true

n%i==0    (6%1==0)    if condition is true

sum=sum+i    (sum=0+1)    So,  sum=1

 i++    (i=1+1)     So  i=2

3.2.  i<n    (2<6)    while loop condition is true

n%i==0    (6%2==0)    if condition is true

sum=sum+i    (sum=1+2)    So,  sum=3

 i++    (i=2+1)     So  i=3

3.3.  i<n    (3<6)    while loop condition is true

n%i==0    (6%3==0)    if condition is true

sum=sum+i    (sum=3+3)    So,  sum=6

 i++    (i=3+1)     So  i=4

3.4.  i<n    (4<6)    while loop condition is true

n%i==0    (6%4==0)    if condition is false

 i++    (i=4+1)     So  i=5

3.5.  i<n    (5<6)    while loop condition is true

n%i==0    (6%5==0)    if condition is false

 i++    (i=5+1)     So  i=6

3.6.  i<n    (6<6)    while loop condition is false

It comes out of the while loop.

  1. sum==n    (6==6)    if condition is true

So it prints 6 is a Perfect Number.

  1. Thus program execution is completed.

For Not a Perfect Number:

  1. Let us assume that a user enters the positive integer value as 4.
  2. It assigns the value of sum=0 and n=4.
  3. It assigns the value of i=1 and the loop continues till the condition of the while loop is true.

3.1.   i<n    (1<4)    while loop condition is true

n%i==0    (4%1==0)    if condition is true

sum=sum+i    (sum=0+1)    So,  sum=1

 i++    (i=1+1)     So  i=2

3.2.  i<n    (2<4)    while loop condition is true

n%i==0    (4%2==0)    if condition is true

sum=sum+i    (sum=1+2)    So,  sum=3

 i++    (i=2+1)     So  i=3

3.3.  i<n    (3<4)    while loop condition is true

n%i==0    (4%3==0)    if condition is false

3.4.  i<n    (4<4)    while loop condition is false

It comes out of the while loop.

  1. sum==n    (3==4)    if condition is false

So it goes to else part and prints 4 is Not a Perfect Number.

  1. Thus program execution is completed.

Output:

perfect number or not

perfect

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

Similar Posts