Found 21 Articles for Java.Util Package

GCD and LCM of two numbers in Java

Chandu yadav
Updated on 25-Jun-2020 11:57:26

893 Views

Following is an example which computes find LCM and GCD of two given numbers.Programimport java.util.Scanner; public class LCM_GCD {    public static void lcm(int a, int b){       int max, step, lcm = 0;       if(a > b){          max = step = a;       } else{          max = step = b;       }       while(a!= 0) {          if(max%a == 0 && max%b == 0) {             lcm = max;             break;          }          max += step;       }       System.out.println("LCM of given numbers is :: "+lcm);    }    public static void gcd(int a,int b){       int i, hcf = 0;          for(i = 1; i

Advertisements