Java program to find g.c.d using recursion

ww‮ditfigi.w‬ea.com

Here's a Java program to find the GCD (Greatest Common Divisor) of two numbers using recursion:

import java.util.Scanner;

public class GCD {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();

        System.out.print("Enter the second number: ");
        int num2 = scanner.nextInt();

        int gcd = findGCD(num1, num2);

        System.out.println("GCD of " + num1 + " and " + num2 + " is " + gcd);
    }

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

In this program, we use recursion to calculate the GCD of two numbers. The findGCD method takes two arguments, num1 and num2, and returns the GCD of these two numbers. If num2 is equal to 0, we return num1, which is the GCD. Otherwise, we call the findGCD method recursively with num2 as the first argument and num1 % num2 as the second argument. This continues until num2 becomes 0.