0% found this document useful (0 votes)
11 views

Program To Calculate LCM

This Java program calculates the least common multiple (LCM) of two numbers input by the user. It uses a while loop to iteratively multiply the larger number by integers until it is divisible by both input numbers, then returns the LCM.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Program To Calculate LCM

This Java program calculates the least common multiple (LCM) of two numbers input by the user. It uses a while loop to iteratively multiply the larger number by integers until it is divisible by both input numbers, then returns the LCM.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CODE

Program to calculate LCM


/*TO CALCULATE LCM-LEAST COMMON MULTIPLE*/

package javaclass.newpackage;

import java.util.Scanner;

public class LCM {

public static void main(String arg[])

long a,b,lcm;

Scanner sc=new Scanner(System.in);

System.out.println("enter 1st number: ");

a=sc.nextLong();

System.out.println("enter 2nd number: ");

b=sc.nextLong();

lcm=lcmCalculation(a,b);

System.out.println("LCM of "+a+" and "+b+" is ="+lcm);

static long lcmCalculation(long n1,long n2)

long temp,i=2,res;

if(n1>n2)

res=n1;

else

res=n2;

temp=res;

while(res%n1!=0 || res%n2!=0)

res=temp*i;

i++;

return res;
} }

OUTPUT

You might also like