# 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");
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("\n Entered number is a perfect number");
Console.ReadLine();
}
else
{
Console.WriteLine("\n Entered number is not a perfect number");
Console.ReadLine();
}
}
}
}```
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.

advertisement
advertisement

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.

Note: Join free Sanfoundry classes at Telegram or Youtube

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.

advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.