0
0
CProgramBeginner · 2 min read

C Program to Check Perfect Number with Output and Explanation

A C program to check a perfect number sums all its divisors using a for loop and compares the sum to the number; if equal, it prints "Perfect number", else "Not perfect number".
📋

Examples

Input6
Output6 is a Perfect number
Input28
Output28 is a Perfect number
Input10
Output10 is Not a Perfect number
🧠

How to Think About It

To check if a number is perfect, find all numbers less than it that divide it evenly using % operator, add those divisors, and then compare the sum to the original number. If they match, the number is perfect.
📐

Algorithm

1
Get input number from user
2
Initialize sum to zero
3
Loop from 1 to number-1
4
If current number divides input evenly, add it to sum
5
After loop, compare sum with input number
6
Print result based on comparison
💻

Code

c
#include <stdio.h>

int main() {
    int num, sum = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    for (int i = 1; i < num; 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 a number: 6 6 is a Perfect number
🔍

Dry Run

Let's trace the input 6 through the code to see how it checks for a perfect number.

1

Input number

num = 6, sum = 0

2

Loop i from 1 to 5

Check divisors and add to sum

3

i=1

6 % 1 == 0, sum = 0 + 1 = 1

4

i=2

6 % 2 == 0, sum = 1 + 2 = 3

5

i=3

6 % 3 == 0, sum = 3 + 3 = 6

6

i=4

6 % 4 != 0, sum = 6

7

i=5

6 % 5 != 0, sum = 6

8

Compare sum and num

sum = 6, num = 6, equal -> Perfect number

inum % i == 0?sum
1true1
2true3
3true6
4false6
5false6
💡

Why This Works

Step 1: Find divisors

The program checks every number less than the input to see if it divides the input evenly using num % i == 0.

Step 2: Sum divisors

All divisors found are added together in the sum variable.

Step 3: Compare sum to number

If the sum of divisors equals the original number, it means the number is perfect.

🔄

Alternative Approaches

Using while loop
c
#include <stdio.h>

int main() {
    int num, sum = 0, i = 1;
    printf("Enter a number: ");
    scanf("%d", &num);
    while (i < num) {
        if (num % i == 0) {
            sum += i;
        }
        i++;
    }
    if (sum == num) {
        printf("%d is a Perfect number\n", num);
    } else {
        printf("%d is Not a Perfect number\n", num);
    }
    return 0;
}
This approach uses a while loop instead of for loop; functionally same but shows different loop style.
Optimized loop up to num/2
c
#include <stdio.h>

int main() {
    int num, sum = 0;
    printf("Enter a 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;
}
Checking divisors only up to half the number improves efficiency because no divisor can be greater than num/2.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops from 1 to n (or n/2 in optimized), checking divisibility each time, so time grows linearly with input size.

Space Complexity

Only a few variables are used regardless of input size, so space complexity is constant.

Which Approach is Fastest?

The optimized approach checking divisors up to n/2 is faster than checking up to n, but both are O(n). Using more advanced math can reduce time further but is more complex.

ApproachTimeSpaceBest For
For loop up to nO(n)O(1)Simple understanding
While loop up to nO(n)O(1)Alternative loop style
For loop up to n/2O(n/2) ~ O(n)O(1)Better efficiency
💡
Check divisors only up to half the number to reduce unnecessary checks.
⚠️
Including the number itself in the sum of divisors, which always makes the sum larger than the number.