Java Program to Find LCM by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Find Even Numbers in an Array by Using Recursion

In this program we are going to see how to find LCM of 2 numbers using Recursion in Java programming language.

Java Program to Find LCM by Using Recursion

Lets assume 2 numbers A = 10, B=15

Common divisor of 10, 15 = 5

Now, 10/5 = 2 and 15/5 = 3

Hence LCM(10,15) = 5*2*3 = 30

Now let’s see different ways to find find LCM of 2 numbers in an Array by using Recursion.

Method-1: Java Program to Find LCM By Using Static Input and Recursion

Approach:

  • Declare and initialize an integer variable ‘a’ as 10
  • Declare and initialize an integer variable ‘b’ as 15
  • Call a user defined method calculateLCM() and pass the ‘a’,‘b’ as parameter.
  • Inside the user defined method we will call gcd() method as “(a/gcd(a,b))*b”.
  • Inside gcd() method we will find the gcd of 2 numbers by “gcd(b % a, a)” and return the value to the calculateLCM() method.
  • Now the value of the user defined method calculateLCM() is stored in an integer variable say ‘lcm’.
  • Print the lcm of 2 numbers.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        //declare and initialize an integer variable a
        int a = 10;
        //declare and initialize an integer variable b
        int b = 15;
        //call the method and store the value inside an integer variable say ‘lcm’
        int lcm = calculateLCM(a,b);
        //print the result
        System.out.println("The LCM of two numbers "+a+", "+b+" is: "+lcm);
    }
    
    //gcd() method
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
     
    // method to return LCM of two numbers
    static int calculateLCM(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }
}
Output:

The LCM of two numbers 10, 15 is: 30

Method-2: Java Program to Find LCM By Using User Input and Recursion

Approach:

  • Create a scanner class.
  • Declare two integer variables say ‘a’, ‘b
  • Prompt the user to enter the numbers for a, b respectively.
  • Call a user defined method calculateLCM() and pass the ‘a’,‘b’ as parameter.
  • Inside the user defined method we will call gcd() method as “(a/gcd(a,b))*b”.
  • Inside gcd() method we will find the gcd of 2 numbers by “gcd(b % a, a)” and return the value to the calculateLCM() method.
  • Now the value of the user defined method calculateLCM() is stored in an integer variable say ‘lcm’.
  • Print the lcm of 2 numbers.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the 1st number:");
        //declare an integer variable ‘a’and initialize it by user input using scanner class.
        int a = s.nextInt();
        System.out.println("Enter the 2nd number:");
        //declare an integer variable ‘b’and initialize it by user input using scanner class.
        int b = s.nextInt();
        //call the method and store the value inside an integer variable say ‘lcm’
        int lcm = calculateLCM(a,b);
        //print the result
        System.out.println("The LCM of two numbers "+a+", "+b+" is: "+lcm);
    }
    
    //gcd() method
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
            return gcd(b % a, a);
    }
     
    // method to return LCM of two numbers
    static int calculateLCM(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }
}
Output:

Enter the 1st number:
78
Enter the 2nd number:
97
The LCM of two numbers 78, 97 is: 7566

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: