Perfection

A perfect number is a positive integer that is equal to the sum of all its positive, proper divisors; for example, 6, which equals 1 + 2 + 3, and 28, which equals 1 + 2 + 4 + 7 + 14, are perfect numbers. A positive number that is not perfect is imperfect and is deficient or abundant according to whether the sum of its positive, proper divisors is smaller or larger than the number itself. Thus, 9, with proper divisors 1, 3, is deficient; 12, with proper divisors 1, 2, 3, 4, 6, is abundant.

Problem Statement:

Given a number, determine if it is perfect, abundant, or deficient.

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

namespace Perfection
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("Enter a positive integer: ");
                int inputNumber = int.Parse(Console.ReadLine());
                int sum = 1, i = 0;
                for (i = 2; i < inputNumber; i++)
                {
                    if (inputNumber % i == 0)
                    {
                        sum = sum + i;
                    }
                }
                if (sum == inputNumber)
                {
                    Console.WriteLine(inputNumber + " is a perfect number.");
                }
                else if (sum > inputNumber)
                {
                    Console.WriteLine(inputNumber + " is an abundant number.");
                }
                else
                {
                    Console.WriteLine(inputNumber + " is a deficient number.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s