# C# Program to Check Whether a Given Number is Perfect Number

«
»

This is a C# Program to check whether the entered number is a perfect number or not.

Problem Description

This C# Program Checks Whether the Entered Number is a Perfect Number or Not.

Problem Solution

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

Program/Source Code

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.

```/*
* C# Program to Check Whether the Entered Number is a Perfect Number or Not
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
class Program
{
static void Main(string[] args)
{
int number,sum=0,n;
Console.Write("enter the Number");
n = number;
for (int i = 1; i < number;i++)
{
if (number % i == 0)
{
sum=sum + i;
}
}
if (sum == n)
{
Console.WriteLine("\n Entered number is a perfect number");
}
else
{
Console.WriteLine("\n Entered number is not a perfect number");
}
}
}
}```
Program Explanation

In this C# program, we are reading the number using ‘n’ variable. A perfect number is a positive integer that is equal to the sum of its proper divisors.

Note: Join free Sanfoundry classes at Telegram or Youtube

Using for loop check the entered number is a perfect number or not, initialize the value of ‘i’ variable as 1 and check the condition that the value of ‘i’ variable is less than the value of ‘number’ variable.

If the condition is true then execute the iteration of the loop,. If condition statement is used to check that the modulus of the value of ‘number’ variable by the value of ‘i’ variable is equal to 0, if the condition is true then execute if condition statement.

Compute the summation of the value of ‘sum’ variable with the value of ‘i’ variable. Using if else condition statement check the condition that the value of ‘sum’ variable is equal to the value of ‘n’ variable.

If the condition is true then execute the statement and print that number is a perfect number. Otherwise, if the condition is false then execute the else statement and print that number is not a perfect number.

Runtime Test Cases
```
Enter the Number : 6
Entered Number is a Perfect Number```

Sanfoundry Global Education & Learning Series – 1000 C# Programs. 