Perfect number in java – Java Program to Check Perfect Number by Using Recursion

Perfect number in java: In the previous article, we have discussed about Java Program to Print N to 1 by Using Recursion

In this program we are going to see how to check perfect number by using recursion in Java programming language.

Java Program to Check Perfect Number by Using Recursion

Perfect Number:

Perfect number java: A number in which the sum of the factors of the number excluding itself is equal to the original number is called as perfect number.

Let’s understand it clearly with an example.

Suppose the number say n = 28
Factors of 28 = 1, 2, 4, 7, 14, 28
Sum of the factors of the number excluding itself = 1+2+4+7+14 = 28
Hence, 28 is a perfect number.

Now let’s see different ways to check Perfect number by using recursion.

Method-1: Java Program to Check Perfect Number By Using Static Input and Recursion

Approach:

  • Declare an integer variable say ‘n’ and initialize the value.
  • Call a user defined method Perfect() method and pass ‘n’ as parameter.
  • Inside the user defined method we will 1st store the original number into a temporary integer variable as “num
  • Inside that Perfect() method we will call another sumofFact() method to find the sum of factors of the number.
  • Now inside sumofFact() method we will check if the factor i > num/2  then we will return the value as 0 else if the number is divisible by 'i' then we will call sumofFact() method recursively i.e. “i+sumofFact(i+1)” if the number is not divisible by 'i' then we will call sumofFact() method recursively i.e. “sumofFact(i+1)” and return the value to the Perfect() method.
  • Now the returned valued call sumofFact() method is stored in an integer variable “value
  • If the sum of factors of the number is equal to the original number then print perfect number else print not a perfect number.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    static int num;
    public static void main(String[] args)
    {
        // declare and initialize an integer variable ‘n’
        int n = 28;
        // calling perfect() method with parameter n
        perfect(n);
    }
    
    //defining perfect() method
    static void perfect(int n)
    {
        // integer num is used to store the original number
        num = n;
        // calling sumofFact() method to find the sum of factors of the number
        int value = sumofFact(1);
        // if the sum of factors of the number except that number is equal to the original number then print Perfect number else print not a perfect number
        if(value==num)
            System.out.println(num+" is a perfect number");
        else
            System.out.println(num+" is not a perfect number");    
    }
    
    //defining sumofFact() method
    static int sumofFact(int i)
    {
        // if the factor is greater than half of the number then return 0 else recursively call sumofFact() method and return the value to the perfect() method
        if(i > num/2)
            return 0;
        else if(num %i==0)
            return i+sumofFact(i+1);
        else
            return sumofFact(i+1);
    }
}
Output:

28 is a perfect number

Method-2: Java Program to Check Perfect Number By Using User Input and Recursion

Approach:

  • Declare an integer variable say ‘n
  • Prompt the user to enter the value of n.
  • Call a user defined method Perfect() method and pass ‘n’ as parameter.
  • Inside the user defined method we will 1st store the original number into a temporary integer variable as “num
  • Inside that Perfect() method we will call another sumofFact() method to find the sum of factors of the number.
  • Now inside sumofFact() method we will check if the factor i > num/2  then we will return the value as 0 else if the number is divisible by 'i' then we will call sumofFact() method recursively i.e. “i+sumofFact(i+1)” if the number is not divisible by 'i' then we will call sumofFact() method recursively i.e. “sumofFact(i+1)” and return the value to the Perfect() method.
  • Now the returned valued call sumofFact() method is stored in an integer variable “value
  • If the sum of factors of the number is equal to the original number then print perfect number else print not a perfect number.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    static int num;
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the number:");
        int n = s.nextInt();
        // calling perfect() method with parameter n
        perfect(n);
    }
    
    //defining perfect() method
    static void perfect(int n)
    {
        // integer num is used to store the original number
        num = n;
        // calling sumofFact() method to find the sum of factors of the number
        int value = sumofFact(1);
        // if the sum of factors of the number except that number is equal to the original number then print Perfect number else print not a perfect number
        if(value==num)
            System.out.println(num+" is a perfect number");
        else
    		System.out.println(num+" is not a perfect number");    
    }
    
    //defining sumofFact() method
    static int sumofFact(int i)
    {
        // if the factor is greater than half of the number then return 0 else recursively call sumofFact() method and return the value to the perfect() method
        if(i > num/2)
            return 0;
        else if(num %i==0)
            return i+sumofFact(i+1);
        else
            return sumofFact(i+1);
    }
}
Output:

Enter the number:
78
78 is not a perfect number

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: