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 user2
Initialize a sum variable to zero3
Loop from 1 to number-1 and check if the current number divides the input evenly4
If yes, add it to the sum5
After the loop, compare sum with the input number6
Print that the number is perfect if they are equal, else print not perfectCode
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
| i | number % i == 0 | sum |
|---|---|---|
| 1 | true | 1 |
| 2 | true | 3 |
| 3 | true | 6 |
| 4 | false | 6 |
| 5 | false | 6 |
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).
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop up to n | O(n) | O(1) | Simple and clear code |
| While loop up to n | O(n) | O(1) | Alternative loop style |
| For loop up to n/2 | O(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.