C# Program to Check Whether the Entered Number is a Perfect Number or Not

Defination :

A perfect number is a positive integer that is equal to the sum of its proper divisors

Here is source code of the C# Program to Check Whether the Entered Number is a Perfect Number or Not. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

Program to Check Whether the Entered Number is a Perfect Number :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace perfactnumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n, sum = 0;
            Console.WriteLine("enter the num:");
            n = Convert.ToInt32(Console.ReadLine());
            for (i = 1; i < n; i++)
            {
                if (n % i == 0)
                    sum = sum + i;
            }
            if (sum == n)
            {
                Console.WriteLine("number is perfect", +n);
            }
            else
            {
               Console.WriteLine("not perfect", +n);
            }
            }
     }
}

Output of Program :

C# Program to Check Whether the Entered Number is a Perfect Number or Not

 

Learn More Tutorial :

Leave a Reply

Your email address will not be published. Required fields are marked *