Q. C# Program to Check Whether the Given Number Is Perfect Number or Not.

Here you will find an algorithm and program in C# programming language to check whether the given number is perfect number or not. First let us understand what Perfect number means.

Explanation : Perfect number is a positive integer equal to the sum of its proper divisors.

For Example : 6 is perfect number because sum of its proper divisor(1, 2, 3) is equal to 6.

Perfect Number Algorithm

START
Step 1 - Input the number.
Step 2 - Find all divisors of the number except the number itself.
Step 3 - If the sum of all divisors of the number is equal to the number, then return true. Else, return false.
STOP


C# Program to Check Perfect Number

using System;
  namespace LFC
  {
    class LFC
    {
      static void Main(string[] args)
      {
      int num=28,sum=0,n;
      n = num;
      for (int i = 1; i < num;i++)
      {
        if (num % i == 0)
        {
          sum=sum + i;
        }
      }
      if (sum == n)
      {
        Console.WriteLine(n+" is a perfect number");
        Console.ReadLine();
      }
      else
      {
        Console.WriteLine(n+" is not a perfect number");
        Console.ReadLine();
      }
    }
  }
}

Output

28 is a perfect number.