---
title: "Write a program to check if a given number is a perfect number or not."  
description: "Write a program to check if a given number is a perfect number or not."  
author: "Utpal Vishwas"  
published: 2023-06-14  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/158752/write-a-program-to-check-if-a-given-number-is-a-perfect-number-or-not  
category: "c#"  
tags: ["c#", "programs"]  
reading_time: 1 minute  

---

# Write a program to check if a given number is a perfect number or not.

Write a [program to check](https://www.mindstick.com/forum/157542/write-a-java-program-to-check-if-a-list-of-integers-contains-only-odd-numbers) if a given number is a perfect number or not.

## Replies

### Reply by Aryan Kumar

Sure, here is the C# code to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a given number is a perfect number or not:

C#

```plaintext
using System;

class PerfectNumber
{
    static void Main(string[] args)
    {
        int number = Convert.ToInt32(Console.ReadLine());
        int sum = 1;

        for (int i = 2; i * i <= number; i++)
        {
            if (number % i == 0)
            {
                if (i * i != number)
                {
                    sum += i + number / i;
                }
                else
                {
                    sum += i;
                }
            }
        }

        if (sum == number)
        {
            Console.WriteLine("{0} is a perfect number.", number);
        }
        else
        {
            Console.WriteLine("{0} is not a perfect number.", number);
        }
    }
}
```

This [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) first reads a number from the user. Then, it uses a for loop to iterate through all the positive divisors of the number. For each divisor, it adds the divisor to a variable called sum. Finally, it checks if the value of sum is equal to the original number. If it is, then the number is a perfect number. Otherwise, the number is not a perfect number.


---

Original Source: https://www.mindstick.com/forum/158752/write-a-program-to-check-if-a-given-number-is-a-perfect-number-or-not

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
