Wednesday, August 6, 2014

To Find the Perfect Number


/*a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
Read here: en.wikipedia.org/wiki/Perfect_number
*/

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

namespace ConsoleApplication2
{
    class Program
    {
        static void PerfectNumber(int a)
        {


            int sum = 0;
            for (int idx = 1; idx < a; idx++)
            {
                int id = a % idx;
                if (id == 0)
                {
                    sum = sum + idx;
                }
              
            }
            Console.WriteLine();
            Console.WriteLine("The sum of all it's proper divisors is: "+sum+'\n');
            if (sum == a)
            {
                Console.WriteLine("YES!  "+a+ " is a Perfect number");

            }
            else
                Console.WriteLine("No!  " + a + " is  NOT a Perfect number");

        }

        static void Main(string[] args)
        {
            Console.WriteLine("********************************************************");
            Console.WriteLine("To Check the whether entered number is Perfect Number !");
            Console.WriteLine();
            Console.Write("Enter the a desired Number:");
            int abc = int.Parse(Console.ReadLine());
            PerfectNumber(abc);

        }
    }
}