0
0
CsharpProgramBeginner · 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 except itself and compares the sum to the number using code like if (sum == number) Console.WriteLine("Perfect number"); else Console.WriteLine("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 the % operator. Add these divisors together. If the total equals the original number, it is perfect; otherwise, it is not.
📐

Algorithm

1
Get the input number from the user
2
Initialize a sum variable to zero
3
Loop from 1 to number-1 and check if the current number divides the input evenly
4
If yes, add it to the sum
5
After the loop, compare sum with the input number
6
Print that the number is perfect if they are equal, else print not perfect
💻

Code

csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());
        int sum = 0;
        for (int i = 1; i < number; i++) {
            if (number % i == 0) sum += i;
        }
        if (sum == number) Console.WriteLine($"{number} is a perfect number");
        else Console.WriteLine($"{number} is not a perfect number");
    }
}
Output
Enter a number: 6 6 is a perfect number
🔍

Dry Run

Let's trace the input 6 through the code

1

Input number

User enters 6

2

Initialize sum

sum = 0

3

Loop and check divisors

Check i=1 to 5: divisors are 1, 2, 3 because 6 % i == 0

4

Sum divisors

sum = 1 + 2 + 3 = 6

5

Compare sum and number

sum (6) == number (6), so number is perfect

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

Why This Works

Step 1: Find divisors

We find all numbers less than the input that divide it evenly using number % i == 0.

Step 2: Sum divisors

We add these divisors to a sum to get the total of all proper divisors.

Step 3: Compare sum to number

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

🔄

Alternative Approaches

Using while loop
csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());
        int sum = 0, i = 1;
        while (i < number) {
            if (number % i == 0) sum += i;
            i++;
        }
        Console.WriteLine(sum == number ? $"{number} is a perfect number" : $"{number} is not a perfect number");
    }
}
Uses a while loop instead of for loop; functionally the same but shows different loop style.
Optimized loop up to number/2
csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());
        int sum = 0;
        for (int i = 1; i <= number / 2; i++) {
            if (number % i == 0) sum += i;
        }
        Console.WriteLine(sum == number ? $"{number} is a perfect number" : $"{number} is not a perfect number");
    }
}
Checks divisors only up to half the number for better performance.

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

Time Complexity

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

Space Complexity

Uses only a few variables, so space is constant regardless of input size.

Which Approach is Fastest?

The optimized approach checking up to n/2 is faster than checking up to n, but both are O(n).

ApproachTimeSpaceBest For
For loop up to nO(n)O(1)Simple and clear code
While loop up to nO(n)O(1)Alternative loop style
For loop up to n/2O(n/2) ~ O(n)O(1)Better performance for large numbers
💡
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.