Java Program to Find G.C.D Using Recursion

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to find G.C.D using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied. Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them.

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter two numbers: 24 and 36

Output

The desired output would be −

The G.C.D of 24 and 36 is 12.

Algorithm

Step 1 - START
Step 2 - Declare three values namely my_input_1, my_input_2 and my_result
Step 3 - Read the required values from the user/ define the values
Step 4 - A recursive function ‘CommonFactor’ is defined which takes two integers as input and returns two values i.e ‘my_input_2’ value and ‘my_input_1’ % ‘my_input_2’ value.
Step 5 - The function is called recursively until the value of ‘my_input_2’ is greater than 0. Store the result.
Step 6 - Display the result
Step 7 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class GCD {
   public static void main(String[] args) {
      int my_input_1, my_input_2, my_result;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the first number : ");
      my_input_1 = my_scanner.nextInt();
      System.out.print("Enter the second number : ");
      my_input_2 = my_scanner.nextInt();
      my_result = CommonFactor(my_input_1, my_input_2);
      System.out.printf("The G.C.D of %d and %d is %d.", my_input_1, my_input_2, my_result);
   }
   public static int CommonFactor(int my_input_1, int my_input_2){
      if (my_input_2 != 0)
         return CommonFactor(my_input_2, my_input_1 % my_input_2);
      else
         return my_input_1;
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the first number : 24
Enter the second number : 36
The G.C.D of 24 and 36 is 12.

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class GCD {
   public static void main(String[] args) {
      int my_input_1, my_input_2, my_result;
      my_input_1 = 24;
      my_input_2 = 36;
      System.out.println("The numbers are defined as " +my_input_1 +" and " +my_input_2);
      my_result = CommonFactor(my_input_1, my_input_2);
      System.out.printf("The G.C.D of %d and %d is %d.", my_input_1, my_input_2, my_result);
   }
   public static int CommonFactor(int my_input_1, int my_input_2){
      if (my_input_2 != 0)
         return CommonFactor(my_input_2, my_input_1 % my_input_2);
      else
         return my_input_1;
   }
}

Output

The numbers is defined as 24 and 36
The G.C.D of 24 and 36 is 12.
raja
Updated on 22-Feb-2022 09:47:11

Advertisements