Question

Program to find least common multiple LCM of two numbers using recursion.

				
					ENTER FIRST NUMBER
25
ENTER SECOND NUMBER
30
LCM OF 25 AND 30 is 150
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class LCM
{

    public static int CalculateLCM(int num1,int num2,int lcm)
    {
        if(lcm%num1==0 && lcm%num2==0)
        {
            return lcm;
        }
        else
        {
            return CalculateLCM(num1,num2,lcm+1);   
        }
    }

    public static void main()
    {
        int firstNum=0,secondNum=0,lcm=0;
        Scanner sc =new Scanner(System.in);
        System.out.println("ENTER FIRST NUMBER");
        firstNum=sc.nextInt();
        System.out.println("ENTER SECOND NUMBER");
        secondNum=sc.nextInt();
        lcm=CalculateLCM(firstNum,secondNum,1);
        System.out.println("LCM OF "+firstNum+" AND "+secondNum+" IS "+lcm);
    }
}



				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *