Java Online Compiler
Example: Java Program to Find LCM of Two Numbers using For loop
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Java Program to Find LCM of Two Numbers using For loop import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int p, q, i, g = 0, x; // p & q - To store the two positive numbers // x - To store the LCM number // g - To store the GCD System.out.println ("-----Enter the two positive integer numbers-----"); p = in.nextInt(); q = in.nextInt(); for (i = 1; i <= p && i <= q; ++i) { if (p % i == 0 && q % i == 0) { g = i; } } x = (p * q) / g; System.out.println ("\nThe LCM of two positive numbers " + p + " & " + q + " is " + x); } }
180 130
Output
Clear
ADVERTISEMENTS