Enter the Number : 6 Entered Number is a Perfect Number
Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Program to Check Whether a Given Number is Perfect Number
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.
This C# Program Checks Whether the Entered Number is a Perfect Number or Not.
A perfect number is a positive integer that is equal to the sum of its proper divisors.
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(); } } } }
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.
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.