Description
Write a Java program to find LCM of given two numbers.LCM stands for Lowest Common Multiple. LCM of a two given number a and b is the smallest positive integer that is divisible by both a and b.For example the LCM of 4 and 6 is 12. Following is the java program to find LCM using while and for loop.
LCM.java(Using While Loop)package com.devglan; public class LCM { public int findLCM(int a, int b){ int lcm = (a > b) ? a : b; while(true){ if (lcm % a == 0 && lcm % b == 0) { break; } lcm++; } return lcm; } public static void main(String [] args){ LCM lcm = new LCM(); System.out.println("LCM Of "+ 4 +" and " + 6 + " is : " + lcm.findLCM(4,6)); } }Using For Loop
public int findLCM(int a, int b){ int lcm = (a > b) ? a : b; for(; lcm <= (a * b) ; lcm++){ if (lcm % a == 0 && lcm % b == 0) { break; } } return lcm; }