Introduction
Perfect number is a positive integer that is equal to the sum of its proper positive divisors. (or)
Perfect number is a number that is half the sum of all of its positive divisors(including itself)
Example:
6 is a Perfect number.
1, 2, 3 are its proper positive divisors and 1 + 2 + 3 = 6 (or)
1, 2, 3,6 are its positive divisors and (1 + 2 + 3 + 6) / 2 = 6
C#.NET Program:
namespace DotNetMirror
{
class PerfectNumber
{
static void Main()
{
Console.WriteLine("/***** Perfect Number Program *****/");
int number, sumOfNumbers = 0, i = 1;
Console.WriteLine("Enter a Number: ");
number = Convert.ToInt16(Console.ReadLine());
while (i < number)
{
if (number % i == 0)
sumOfNumbers = sumOfNumbers + i;
i++;
}
if (sumOfNumbers == number)
{
Console.WriteLine("{0} is a Perfect Number", number);
}
else
{
Console.WriteLine("{0} is Not a Perfect Number", number);
}
Console.ReadLine();
}
}
}
VB.NET Program:
Namespace DotNetMirror
Class PerfectNumber
Shared Sub Main()
Console.WriteLine("/***** Perfect Number Program *****/")
Dim number As Integer, sumOfNumbers As Integer = 0, i As Integer = 1
Console.WriteLine("Enter a Number: ")
number = Convert.ToInt16(Console.ReadLine())
While i < number
If number Mod i = 0 Then
sumOfNumbers = sumOfNumbers + i
End If
i += 1
End While
If sumOfNumbers = number Then
Console.WriteLine("{0} is a Perfect Number", number)
Else
Console.WriteLine("{0} is Not a Perfect Number", number)
End If
Console.ReadLine()
End Sub
End Class
End Namespace
Output: