Here in this program, we will write a code which checks whether the entered number is a Perfect number or not.

What is a Perfect number?

When a positive integer is equal to the sum of its all possible divisors except itself, is known as a perfect number.

For example, 6 is a perfect number because the sum of its all divisors is equal to 6

Divisors of 6 are 1, 2, 3 exclude 6.

Sum of 6 all divisors = 1+2+3 = 6

Statements to use:

  • For loop
  • If statements
  • Else statement

Steps:

  • First, we will ask the user to enter a positive integer
  • Then we start a loop from 1 to the half of the entered number
  • Inside the loop, we will check which number is the divisor of the entered number.
  • If the number is a divisor of the entered number, we will sum it.
  • At last, we will check whether the summing of all the divisors is equal to the entered number or not
  • If it is equal to the entered number, we will print It is a perfect number, is not we will print it is not a perfect number.

Perfect Number in C

#include<stdio.h>
#include<conio.h>
void main()
{
int num, sum=0,i;
clrscr();
printf("Enter a Positive Integer: ");
scanf("%d",&num);
if (num==1)
    {
    printf("It is not a Perfect number");
    }
else
    {
    for(i=1;i<=num/2;i++)
    {
        if(num%i==0)
            sum+=i;
    }
    if(sum==num)
        printf("It is a perfect number");
    else
        printf("It is not a perfect number");
 }
getch();
}

Output:

Enter a Positive Integer: 6

It is a perfect number

People Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *