;

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


Tutorialsrack 11/08/2019 C# Programs

In this C# program, we will learn how to write a program to check whether the entered number is a perfect number or not.

What is a Perfect Number?

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. 

For Example, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.

The sum of divisors of a number, excluding the number itself, is called its aliquot sum, so a perfect number is one that is equal to its aliquot sum. In other words, a perfect number is a number that is half the sum of all of its positive divisors including itself

I.e. σ1(n) = 2n

For Example, 28 is perfect as 1 + 2 + 4 + 7 + 14 + 28 = 56 = 2 × 28

Here is the code of the program to check whether the entered number is a perfect number or not:

Code - C# Program To Check Whether the Entered Number is a Perfect Number or Not
using System;

namespace TutorialsrackPrograms
{
    class Program
    {
        //C# Program to Check Whether the Entered Number is a Perfect Number or Not.
        static void Main(string[] args)
        {
            int number, sum = 0, n;
            Console.Write("Enter The Number: ");
            number = int.Parse(Console.ReadLine());
            n = number;
            for (int i = 1; i < number; i++)
            {
                if (number % i == 0)
                {
                    sum = sum + i;
                }
            }
            if (sum == n)
            {
                Console.WriteLine("\nEntered Number is a Perfect Number");
            }
            else
            {
                Console.WriteLine("\nEntered Number is not a Perfect Number");
            }
            Console.Read();
        }
    }
}
Output

Enter The Number: 28

Entered Number is a Perfect Number


Related Posts



Comments

Recent Posts
Tags