Home » C program to Check Perfect Number

C program to Check Perfect Number

Learn about C program to Check Perfect Number in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C program to Check Perfect Number

In C, any number can be the perfect number if the sum of its positive divisors, excluding the number itself, equals the number.

For example, 6 is a perfect number since it is divisible by one, two, three, and six. So the sum of these values is 1+2+3 = 6 (remember, we have to omit the number itself.) That’s why we didn’t include 6 here. Perfect numbers include 6, 28, 496, and so on.

Program:

 #include<stdio.h>
 int main()
 {
     int num, sum=0;

     printf("Enter Number: ");
     scanf("%d",&num);

     for(int i=1; i<=num/2; i++)
     {
         if(num%i==0)
             sum+=i;
     }

     if( sum==num )
         printf("%d is a perfect number.\n",num);
     else
         printf("%d is not a perfect number.\n",num);

     return 0;
 }

Output:

Enter Number: 28
28 is a perfect number.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C program to Find palindrome number in a given range
Palindrome program in C using while loop