Frequently Asked Java Program 28: Java Program to Find if Given Number Is a Perfect Number

Perfect Number:

A Perfect number is a positive integer that is equal to the sum of its proper divisors excluding the number itself.

For Example: Take Number 6. Its divisors are 1, 2 , 3 and 6. When we add all divisors except 6 ( as it is number itself) (i.e. 1+2+3= 6), it is equal to actual number. So we can say Number 6 is a perfect number.

The smallest perfect number is 6. Other perfect numbers are 28, 496, and 8,128.

Logic:

  1. When we divide a number say X by other number say Y and reminder is zero, then number Y is a proper divisor. All proper divisor of a number X are between 1 and X/2. ( 1 and X/2 are included) . To find the proper divisor we need to divide the number by 1 to number/2.
  2. We need to keep adding all divisors.

Java Program:

package StringPrograms;

import java.util.Scanner;

public class PerfectNumber {
	
	
	
	
	public static void perfectNumber(int userInput)
	{
		// Handling numbers less than or equal to zero
		if(userInput<=0)
		{
			System.out.println("A perfect number must be positive and greater than zero. So kindly enter number greater than zero.");
		}
		else
		{
			int sum =0;
			// Iterating till half of the number
			for(int i = 1; i<= userInput/2;i++)
			{
				// If number is divisible, add divisor.
				if(userInput%i==0)
				{
					sum = sum+i;
				}
			}
			
			// If sum is equal to number
			if(userInput==sum)
			{
				System.out.println(userInput+" is a perfect number.");
			}
			else
			{
				System.out.println(userInput+" is not a perfect number.");
			}
		}
	}

	public static void main(String[] args) {

		// User input for the number
		Scanner sc = new Scanner(System.in);
				
		
		System.out.println("Please enter the number to check for perfect number : ");

		int userInput = sc.nextInt();
		sc.close();

		System.out.println("You entered: " + userInput);
		

		perfectNumber(userInput);
		
		
		

	}
}

Output:

[java]
Please enter the number to check for perfect number :
6
You entered: 6
6 is a perfect number.
=====================================================
Please enter the number to check for perfect number :
1
You entered: 1
1 is not a perfect number.
====================================================
Please enter the number to check for perfect number :
-10
You entered: -10
A perfect number must be positive and greater than zero. So kindly enter number greater than zero.
======================================================
Please enter the number to check for perfect number :
10
You entered: 10
10 is not a perfect number.
[/java]

#HappyCoding

Leave a Reply

Your email address will not be published. Required fields are marked *