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 :: Syntax of Creating Java Map Objects 
Java :: arrays vs collections 
Java :: java stream index 
Java :: java garbage collection 
Java :: @restcontroller 
Java :: android click button programmatically 
Java :: how to add random numbers to an array 
Java :: easter egg 
Java :: java creating an arraylist 
Java :: compare 2 hashmap 
Java :: zoneddatetime java 
Java :: merge sort recursion java 
Java :: how to set default focus on edittext in android 
Java :: void * to int 
Java :: java inheritance example 
Java :: object in java 
Java :: authentication in spring boot 
Java :: prime factors of a number 
Java :: java mahout get clusters centers 
Java :: create fragment constructor in arrayadapter 
Java :: spring aop xml definition 
Java :: foreach() java 
Java :: android java close keyboard 
Java :: worldedit api copy schematic 
Java :: how to create arraylist in java 
Java :: primitive vs wrapper classes in java 
Java :: Get directory in android java 
Java :: how to split string with dolor sign in java 
Java :: mincraft command in plugin 
Java :: java catch stop signal 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =