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
The LCM = 27
No comments:
Post a Comment