Hello Learner
Contributor
Stats
 Views : 9762 Comments : 0 Rating : 0
Actions

# .NET program to check Perfect Number or Not

By Ashok Nalam on 01 Jul 2013 | Category: C# | Tagged: .NET VB.NET number
This snippet checks user input number is Perfect Number or Not using C#.NET and VB.NET programs.
 Rate this resource

### 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: ");
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);
}
}
}
}```

#### 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: ")
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
End Sub
End Class
End Namespace```

#### Output:

 Rate this resource