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

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

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

Perfect divisors are all the numbers which leave zero as remainder when dividing. As per the problem statement here you have to find the product of all perfect divisors of a number.

For Example:

Suppose the number = 55
Perfect divisors of 55 = 1, 5, 11
Product of perfect divisors of 55 = 1*5*11 = 55

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

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

Approach:

• Declare and initialize an integer variable.
• Pass the number and its half into the user defined method divisorProd( ).
• The method decrements the divisor for each recursive call and goes on printing the perfect divisor and returns the product at the end.
• Print the product.

Program:

import java.util.*;
// Main class
public class Main
{
// Recursive method to find product of perfect divisors
public static int divisorProd(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 * divisorProd(num,x-1);
}
else
// Recursively calls the function by decrementing the divisor
return divisorProd(num,x-1);
}

public static void main(String[] args)
{
int num = 55;
System.out.println("Perfect divisors of "+num+" are");
// Finding the perfect divisors by calling the method and storing the prod
int prod = divisorProd(num,num/2);
// Print the result
System.out.print("Product = "+prod);
}
}

Output:

Perfect divisors of 55 are
11 * 5 * 1
Product = 55

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

Approach:

• Declare an integer variable and ask the user to enter a number as value.
• Pass the number and its half into the user defined method divisorProd( ).
• The method decrements the divisor for each recursive call and goes on printing the perfect divisor and returns the product at the end.
• Print the product.

Program:

import java.util.*;
// Main class
public class Main
{
// Recursive method to find product of perfect divisors
public static int divisorProd(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 * divisorProd(num,x-1);
}
else
// Recursively calls the function by decrementing the divisor
return divisorProd(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 prod
int prod = divisorProd(num,num/2);
// Print the result
System.out.print("Product = "+prod);
}
}


Output:

Enter a number
32
Perfect divisors of 32 are
16 * 8 * 4 * 2 * 1
Product = 1024

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.