C Program to check Perfect number or not

Write a C program to find number is Perfect number or not.

In this program we will find number is a perfect number or not using C programming. so we will use of while loop and if else statement. Basically perfect number is a positive number which is equal to the sum of all its divisors excluding itself. we have to find all divisors of that number and find their sum, if sum of divisors is equal to number it means number is Perfect Number. Else sum is not equal to number it mean number is not a perfect number.

Ex:- Enter any number 6
6 is a perfect number as 1 + 2 + 3 = 6.

Number is 15
15 is not a perfect number because 1+3+5=9

Perfect number or not in C programming

Working:-

Step 1- enter the number to be check
Step 2- initialize i with 1.
Step 3- now execute the while loop, while i is less than the number so calculate following expression:
        (i) if number is divided by the i, so add number with the total and store it in total
        (ii)increment the i with 1.
    When i is equal to or greater than the number so loop will terminate.
Step 4- now compare the entered number with the total number.
Step 5- if the total number is equal to entered number so the number is perfect number.
Step 6- Stop

Competitive Coding Techniques

C Progrom:-

#include<stdio.h>
int main()
{
// Initialization of variables
int number,i=1,total=0;

// To take user input
printf("Enter a number: ");
scanf("%d",&number);

while(i<number)
{
if(number%i==0)
{
total=total+i;
i++;
}
}
//to condition is true
if(total==number)
//display
printf("%d is a perfect number",number);
//to condition is false
else
//display
printf("%d is not a perfect number",number);

return 0;
}

Output:-

Enter a number: 28

28 is a perfect number


Enter a number: 153

153 is not a perfect number

3 comments on “C Program to check Perfect number or not”


  • 17-485

    perfect number using python:

    n = int(input(“Enter any number: “))
    sum1 = 0
    for i in range(1, n):
    if(n % i == 0):
    sum1 = sum1 + i
    if (sum1 == n):
    print(“The number is a Perfect number!”)
    else:
    print(“The number is not a Perfect number!”)