# Java program to calculate the GCD of a given number using recursion

Object Oriented ProgrammingJava8Java Programming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

You can calculate the GCD of given two numbers, using recursion as shown in the following program.

## Example

import java.util.Scanner;
public class GCDUsingRecursion {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number :: ");
int firstNum = sc.nextInt();
System.out.println("Enter second number :: ");
int secondNum = sc.nextInt();
System.out.println("GCD of given two numbers is ::"+gcd(firstNum, secondNum));
}

public static int gcd(int num1, int num2) {
if (num2 != 0){
return gcd(num2, num1 % num2);
} else{
return num1;
}
}
}

## Output

Enter first number ::
625
Enter second number ::
125
GCD of given two numbers is ::125