Skip to content

Instantly share code, notes, and snippets.

@aadipoddar
Created April 14, 2022 13:21
Show Gist options
  • Save aadipoddar/fdadadf8262fc1fd558fc731094219b8 to your computer and use it in GitHub Desktop.
Save aadipoddar/fdadadf8262fc1fd558fc731094219b8 to your computer and use it in GitHub Desktop.
Find the LCM and HCF using Recursion
/*
Program to Find the LCM and HCF using Recursion
*/
import java.util.Scanner;
class HCM_LCM_Recursion {
int common = 0;
int calculateLCM(int a, int b) {
common += b;
if (common % a == 0)
return common;
else
return calculateLCM(a, b);
}
int calculateHCF(int a, int b) {
if (b == 0)
return a;
return calculateHCF(b, a % b);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number");
int a = sc.nextInt();
System.out.println("Enter the second number");
int b = sc.nextInt();
System.out.println("LCM is: " + new HCM_LCM_Recursion().calculateLCM(a, b));
System.out.println("HCF is: " + new HCM_LCM_Recursion().calculateHCF(a, b));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment