Write a java program to check the perfect number

Java program to check the perfect number. Write a program that takes a positive integer and returns true if the number is the perfect number. A positive integer is called a perfect number if the sum of all its factors (excluding the number itself, i.e., proper divisor) is equal to its value.

For example, the number 6 is perfect because its proper divisors are 1, 2, and 3, and 6=1+2+3; but the number 10 is not perfect because its proper divisors are 1, 2, and 5, and 1+2+5 is not equal to 10

Input and Output Format

  • Input consists of an integer.
  • Output consists of TRUE / FALSE.

Refer sample output for formatting specifications

Sample Input 1:
28

Sample Output 1:
TRUE

Java program to check the perfect number

Following are the steps to check whether a number is a perfect number or not:

  • Declare and initialize a variable to 28. Pass it to perfectNumber() method.
  • Inside the method, initialize a variable n1 to 0 and a boolean variable and set it false.
  • Now, iterate over numbers from 1 to n and check if it is a divisor if it is then add it to the variable i.e n1.
  • At last, check if the value in n1 is equal to the input number then it is a perfect number else it is not.
package com.demo;
 
import java.util.*;
 
public class Main {
 
public static Scanner sc;
 
public static void main(String[] args) {
int n=28;
System.out.println(perfectNumber(n));
}
public static boolean perfectNumber(int n) {
int n1=0;
boolean b=false;
for(int i=1;i<n;i++)
if(n%i==0)
n1+=i;
System.out.println(n1);
if(n1==n)
b=true;
return b; }
 
}

Output

Check Perfect Number in Java Using While loop

Following is another approach we will use to check if the number is a perfect number or not:

  • Declare and initialize a variable to 28. Pass it to perfectNumber() method.
  • Inside the method, declare variable rem and sum and initialize them to 0. Go through the number from 1 to n (i.e input number). Check for the divisor, if it is a divisor then add it to the variable sum.
  • At last, just check if the value in sum is equal to the input number then it is a perfect number otherwise not.
package com.demo;

public class PerfectNumber {
public static void main(String[] args) {
int n = 28;
perfectNumber(n);
}

public static void perfectNumber(int n) {
int sum = 0;
int i = 1;
while(i < n )
{
if(n % i == 0)
sum = sum + i;
i++;
}
if(sum == n)
System.out.println("It is a perfect number");
else
System.out.println("It is not a perfect number");
}
}

Output

Print Perfect Number between two intervals

Write a program to print prime numbers between two intervals. Ask the user to input the minimum and maximum values. Display all the perfect numbers between these two intervals.

Input and Output Format

  • Input consists of min and max value.
  • Output consists of a list of perfect numbers between two intervals.

Refer sample output for formatting specifications

Sample Input 1:
1

1000

Sample Output 1:

Perfect Numbers between 1and 1000 are :
6

28

496

Following are the steps to print the perfect number between two intervals:

  • Input two intervals(min & max) from the user. Pass them to printPerfectNumber().
  • Inside the method, use for loop and go through min value to max value. Inside this loop, go through the number 1 to n. Check if it is a divisor, if it is then add it to the sum variable.
  • At last, check if the number is equal to the sum, then print all the perfect numbers.
package com.demo;


import java.util.Scanner;

public class PerfectNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int min = sc.nextInt();
        int max = sc.nextInt();
        printPerfectNumber(min,max);
    }

    private static void printPerfectNumber(int min, int max) {
        int n, sum ;
        System.out.println("Perfect Numbers between "+min+ "and "+max+ " are :");
        for(n = min; n <= max; n++)
        {
            sum = 0;
            for(int i = 1; i <= n/2; i++)
            {
                if(n %i == 0)
                    sum = sum + i;
            }
            if(sum == n)
                System.out.println(n+ "");
        }
    }
}

Output

Thus, in this way, we can find whether a number is a perfect number or not in Java.