Beta Coder

Learn code like Beta Coder.

Breaking

Saturday, September 26, 2020

JAVA Program to Find LCM of Two Numbers

Find LCM of Two Numbers

JAVA TO FIND LCM OF TWO NUMBERS
In this program we learn how to calculate LCM of two numbers by provided user using Java.
Java program to calculate LCM of two numbers
// FIND LCM OF TWO NUMBERS
import java.util.*;
class Lcm
{
	int a, b;
	public
		void getdata()
		{
			Scanner in = new Scanner(System.in);
			System.out.print("\n Enter two number : ");
			a = in.nextInt();
			b = in.nextInt();
		}
		int clcm()
		{
			int big, small, i;
			if(a > b)
			{
				big = a;
				small = b;
			}
			else
			{
				big = b;
				small = a;
			}
			for(i = 1; i <= big; i++)
				if((big * i) % small == 0)
					break;
			return(big * i);
		}
		void display(int p)
		{
			System.out.println("The LCM = "+p);
		}
	public static void main(String arg[])
	{
		Lcm l = new Lcm();
		int p;
		l.getdata();
		p = l.clcm();
		l.display(p);
	}
};
OUTPUT:
   Enter two numbers : 27 9
   The LCM = 27

No comments:

Post a Comment