HCF and LCM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class HcfNum{ private int Int; private int Int2; HcfNum(int x, int y){ Int = x; Int2 = y; } private int HcfIs(){ int temp,temp2; temp = Math.max(Int,Int2); temp2 = Math.min(Int, Int2); while(temp!=0 && temp2!=0){ int rem = temp%temp2; if(rem == 0) { break; } int chng = temp2; temp2 = temp%temp2; temp = chng; } return temp2; } public void example(){ System.out.println("HCF is " + HcfIs()); } } |
LCM
LCM can be calculated using a*b/HCF