C Program to Check Whether a Number is Perfect Number or Not

In this C program, we will check whether a number is perfect number or not. A number is a perfect number, if the sum of all the divisors of a number is equal to the number itself.

Algorithm to check a number is perfect number or not
  • Take a number N as input from user.
  • Find all divisors of a N between 1 to N/2.
  • Add the values of all divisors to a variable sum.
  • If sum is equal to N, then N is a perfect number otherwise not a perfect number.

C program to check a number is perfect number

#include<stdio.h>

int main () {
    int num, i, divSum;

    printf("Enter a number\n");
    scanf("%d", &num);

    for (divSum = 0, i = 1; i <= num/2; i++) {
        if (num % i == 0) {
            divSum += i;
        }
    }
    
    /* Check if Divisor sum is equal to input number */
    if (divSum == num)
        printf("%d is a Perfect Number\n", num);
    else
        printf("%d is Not a Perfect Number\n", num);

 return 0;
}
Output
Enter a number
10
10 is Not a Perfect Number
Enter a number
6
6 is a Perfect Number

Related Topics
C program to find perfect numbers between 1 to N using for loop
C program to find sum of prime numbers between 1 to N
C program to find sum of prime numbers between 1 to N
C program to check whether a number is magic number
C program to check whether a number is prime or not
C Program to calculate factorial of a number
C program to check a number is palindrome or not
C program to find all roots of quadratic equation
C program to find gcd of two numbers
List of all C programs