DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

.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.
  Discuss Add Comment   |  
Add rating 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: ");
            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:


  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 8666  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.