;

# Java Program to Find HCF or GCD of Two Numbers Using Recursion

###### Tutorialsrack 23/05/2021 Java

In this Java program, you’ll learn how to Find the HCF and GCD of two Numbers using the recursion.

Java Program to Find HCF or GCD of Two Numbers Using Loops and Library Function

Java Program to Find HCF or GCD of Two Numbers Using the Euclidean Algorithm and Recursion

What is Recursion?

Recursion is a process in which a function calls itself directly or indirectly and the corresponding function is called a recursive function.

##### Java Program to Find HCF or GCD of Two Numbers Using Recursion
``````// Java Program to Find GCD of two Numbers Using Recursion

import java.util.Scanner;

public class JavaPrograms {

//Recursive Function
public static int getGCD(int a, int b) {
if (a == 0)
return b;
else
return getGCD(b % a, a);
}

public static void main(String[] args) {

// scanner class declaration
Scanner sc = new Scanner(System.in);
// input from the user
System.out.print("Enter the first number : ");
int num1 = sc.nextInt();
// input from the user
System.out.print("Enter the second number : ");
int num2 = sc.nextInt();

// Calling Recursive Function
int GCD = getGCD(num1, num2);

System.out.print("HCF of " + num1 + " and " + num2 + " is " + GCD);

// closing scanner class(not compulsory, but good programming practice)
sc.close();

}
}``````
##### Output

Enter the first number : 48

Enter the second number : 18

HCF of 48 and 18 is 6

Remove This Line it is needed for escape from div