C Program to Find if the Given Number is Perfect Number or Not
The following example shows, find whether a number is a perfect number.
- Takes an integer input from the user.
- Loops from 1 to one less than the number to find proper divisors.
- Calculates the sum of all proper divisors of the number.
- Checks if the sum equals the number to determine if it's a perfect number.
Example : pgm.c
#include <stdio.h> int main() { int num, sum = 0; // Input a number printf("Enter a number: "); scanf("%d", &num); // Find sum of proper divisors for (int i = 1; i < num; i++) { if (num % i == 0) { sum += i; } } // Check if the number is perfect 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 a number: 6
6 is a Perfect Number.
6 is a Perfect Number.