Q:

Java - Calculate LCM (Least Common Multiple) using Java Program

belongs to collection: Java Basic Programs

0

Java - Calculate LCM (Least Common Multiple) using Java Program

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Calculate LCM (Least Common Multiple) of Numbers using Java Program

import java.util.Scanner;
 
public class Lcm {
  
    //  Lowest common Number
    public static int lcm(int First_number, int Second_number) {
        int x,max=0,min=0,lcm=0;
        if(First_number>Second_number)
        {
            max=First_number;
            min=Second_number;
        }
        else
        {
            max=Second_number;
            min=First_number;
        }
          
     
          
        for(int i=1;i<=min;i++)
           {
            x=max*i; 
            if(x%min==0) 
             {
              lcm=x; 
              break; 
             }
            }
         return lcm;
    }
  
    public static void main(String[] args) {
      
        Scanner sc=new Scanner(System.in);
 
        System.out.print("First Number  :");
        int num1=sc.nextInt();
        System.out.print("Second Number :");
        int num2=sc.nextInt();
 
        System.out.println("Lowest Common Factor: " + lcm(num1,num2));
       
    }
}

Output

    First Number  :24
    Second Number :16
    Lowest Common Factor: 48

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to calculate HCF of two numbers... >>
<< Java - Greatest Common Factor or Euclidean Algorit...