Skip to main content

Posts

Showing posts with the label modulo

program for finding lcm and hcf of two numbers using modulo operator in euclidean algorithm in java

 hcf of the numbers Code:: import java.util.*; import java.lang.*; import java.io.*; //hcf of two numbers  //by efficient use of modulo operator in euclidean algorithm class Test { public static void main(String[] args){ int num1=36,num2=60,hcf; hcf = getHcf(num1,num2); System.out.println("the hcf : "+hcf); } public static int getHcf(int a,int b){ return b==0 ? a : getHcf(b,a%b); } } LCM of two numbers property  if we have 2 numbers num1,num2 and there lcm is l and hcf is h then num1 * num2 = l * h Code:: import java.util.*; import java.lang.*; import java.io.*; //lcm of two numbers  //by efficient use of modulo operator in euclidean algorithm class Test { public static void main(String[] args){ int num1=36,num2=60,hcf; hcf = getHcf(num1,num2); int lcm = (num1*num2)/hcf; System.out.println("the lcm: "+lcm); } public static int getHcf(int a,int b){ return b==0 ? a : getHcf(b,a%b); } }