Java Program to Find the Sum of All Perfect Divisors of a Number Using Recursion

In the previous article, we have discussed about Java Program to Reverse a Stack by Using Recursion

In this article we are going to see how we can find the sum of all perfect divisors of a number using recursion by Java programming language.

Java Program to Find the Sum of All Perfect Divisors of a Number Using Recursion

Perfect divisors are all the numbers which leave zero as remainder when dividing.

Let’s see the program to understand it more clearly.

Method-1: Java Program to Find the Sum of All Perfect Divisors of a Number By Using Recursion & Static Input Value

Approach:

  • Store a number in a variable.
  • Pass the number and its half into the user defined method divisorSum() as parameter.
  • The method decrements the divisor for each recursive call and goes on printing the perfect divisor and returns the sum at the end.
  • Print the sum.

Program:

import java.util.*;
// Main class
public class Main
{
    // Recursive method to find sum of perfect divisors
    public static int divisorSum(int num, int x)
    {
        // If the divisor reaches 1
        if(x==1)
        {
            // Prints the divisor
            System.out.println(x+" ");
            return 1;
        }
        // If x is a perfect divisor
        if(num%x==0)
        {
            // Prints the divisor
            System.out.print(x+" + ");
            // Recursively calls the function by decrementing the divisor
            return x + divisorSum(num,x-1);
        }
        else
            // Recursively calls the function by decrementing the divisor
            return divisorSum(num,x-1);
    }

    public static void main(String[] args)
    {
        int num = 55;
        System.out.println("Perfect divisors of 55 are:");
        // Check if the number is divisible by 9
        int res = divisorSum(num,num/2);
        // Print the result
        System.out.print("Sum = "+res);
    }
}
Output:

Perfect divisors of 55 are:
11 + 5 + 1 
Sum = 17

Method-2: Java Program to Find the Sum of All Perfect Divisors of a Number By Using Recursion & User Input Value

Approach:

  • Ask the user to enter a number and store it.
  • Pass the number and its half into the user defined method divisorSum( ) as parameter.
  • The method decrements the divisor for each recursive call and goes on printing the perfect divisor and returns the sum at the end.
  • Print the sum.

Program:

import java.util.*;
// Main class
public class Main
{
    // Recursive method to find sum of perfect divisors
    public static int divisorSum(int num, int x)
    {
        // If the divisor reaches 1
        if(x==1)
        {
            // Prints the divisor
            System.out.println(x+" ");
            return 1;
        }
        // If x is a perfect divisor
        if(num%x==0)
        {
            // Prints the divisor
            System.out.print(x+" + ");
            // Recursively calls the function by decrementing the divisor
            return x + divisorSum(num,x-1);
        }
        else
            // Recursively calls the function by decrementing the divisor
            return divisorSum(num,x-1);
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        // Ask the user for input
        System.out.println("Enter a number");
        int num = sc.nextInt();
        System.out.println("Perfect divisors of "+num+" are");
        // Finding the perfect divisors by calling the method and storing the sum
        int sum = divisorSum(num,num/2);
        // Print the result
        System.out.print("Sum = "+sum);
    }
}
Output:

Enter a number
64
Perfect divisors of 64 are
32 + 16 + 8 + 4 + 2 + 1 
Sum = 63

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Programs: