Program to check given number is Perfect number in C

C Program to check given number is Perfect number


In this tutorial you will learn how to write a program in C programming language to check a given number is perfect or not.

This program is little bit tricky and logical.


But Before directly moving on writing and learning the program first you should know 

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.


Lets understand it with example

6 is a positive number and its divisor is 1,2,3 and 6 itself.

But we should not include 6 as by the definition of perfect number.

Lets add its divisor excluding itself

1+2+3 = 6 which is equal to number itself.

It means 6 is a Perfect Number.

How our program will behave?

C program for perfect number is written below. This program will take a positive number as an input.


Suppose you want to check that a number is perfect or not then you have to give that number as an input to this below program at time of execution.


After the calculation it will give you appropriate output.

If number is perfect then output will be number is perfect. And if number is not perfect then output will be number is not perfect. 

Below is a program to check a given number is perfect or not in C

void main() {
   int reminder, sum=0, i, originalNum;
    printf("please enter number: ");
    for (i = 1; i <= originalNum/2; i++)
            reminder = originalNum % i;
	        if (reminder == 0)
                sum = sum + i;
        if (sum == originalNum)
            printf("given no. is perfect number");
            printf("given no. is not a perfect number");


perfect number program in c

Explanation of the above program

  • In the above program there are 4 variables. originalNum, sum, i, reminder, variable is to store the value given as an input. 
  • Main login is written in the body of for loop.
  • Loop will execute half times of the given input number. As a number can only divide itself by half of its number and itself.
  • Now inside for loop there is a if statement. And body of if statement is adding the number which is divisor of a given input.
  • originalNum % i is a logic to check the divisor of a number. if originalNum % i return 0 it means that i can divide the given number so we can consider it as it is a divisor of given number.
  • And we will add all divisor to get sum of divisor.
  • At last we will check using if statement that if sum of divisor is equal to its original number or not. If equal it means that the given number is Perfect Number otherwise it is not a perfect number

I hope above tutorial has helped you to understand program of perfect number using C.

Leave a Comment