GCD means Greatest Common Divisor and HCF means Highest Common Factor
For 26 and 52 GCD is 26
For 26 and 39 GCD is 13
For 13 and 53 GCD is 1
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class GCDHCF{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int num1 = 39, num2 = 65; System.out.print(gcd(num1, num2)); } public static int gcd(int num1, int num2){ if(num1==0) return num2; if(num2==0) return num1; if(num1==num2) return num1; if(num1>num2) return gcd(num1-num2, num2); return gcd(num1, num2-num1); } } |
Python
def gcd(num1, num2): if num1==0: return num2 if num2==0: return num1 if num1==num2: return num1 if num1>num2: return gcd(num1-num2, num2) return gcd(num1, num2-num1) num1, num2 = 39, 65 print(gcd(num1, num2))