### 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:

- 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.
- We need to keep adding all divisors.

Java Program:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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:

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.

#HappyCoding

#### Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning