Write a program to compute GCD of 2 numbers using recursion.
Input and Output Format:
Input consists of 2 integers.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.
Sample Input and Output:
Enter two positive integers
36
27
G.C.D of 36 and 27 = 9
Solution
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter two positive integers");
int n, n1;
n = s.nextInt();
n1 = s.nextInt();
System.out.println("G.C.D of " + n + " and " + n1 + " = " + GCD(n1, n));
}
public static int GCD(int n1, int n2) {
if (n1 != 0)
return GCD(n2 % n1, n1);
else
return n2;
}
}
Happy Learning – If you require any further information, feel free to contact me.