Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

lcm of two number in java

public class Main {
  public static void main(String[] args) {

    int n1 = 72, n2 = 120, gcd = 1;

    for(int i = 1; i <= n1 && i <= n2; ++i) {
      // Checks if i is factor of both integers
      if(n1 % i == 0 && n2 % i == 0)
        gcd = i;
    }

    int lcm = (n1 * n2) / gcd;
    System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
  }
}
Comment

lcm of two number in java

public class Main {
  public static void main(String[] args) {

    int n1 = 72, n2 = 120, lcm;

    // maximum number between n1 and n2 is stored in lcm
    lcm = (n1 > n2) ? n1 : n2;

    // Always true
    while(true) {
      if( lcm % n1 == 0 && lcm % n2 == 0 ) {
        System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);
        break;
      }
      ++lcm;
    }
  }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to remove last latter in word in java 
Java :: string into ascii in java 
Java :: java casting to int 
Java :: java streams 
Java :: convert from integer to character java 
Java :: get ocurrences in array java 
Java :: add random numbers to array 
Java :: super class tostring java 
Java :: how to get length of 2d array java 
Java :: switch in java 
Java :: java to kotlin converter android studio 
Java :: java servlet life cycle 
Java :: java @override annotation 
Java :: using super in java 
Java :: list of list in java 
Java :: how to divide a double in java 
Java :: spring boot basic authentication 
Java :: mcq java 
Java :: how to use another class in java inside of an action listenrer 
Java :: fragment to activity typecasting 
Java :: colors java intell print 
Java :: java import keyword 
Java :: efficient java code to reverse array elements 
Java :: key caracter from code java 
Java :: java mcq test 
Java :: add external JARs to java vscode 
Java :: set maven goals in eclipse 
Java :: How can I add a listener on the ok button of JOptionPane 
Java :: setlist arraylist java swing example 
Java :: linearview 2 items next to each other 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =