Write Java Source Code on Printing the Greatest Common Divisor or GCD in Recursion
Sample Output:Enter the base number: 100
Enter the power: 45
The GCD of 2 numbers is: 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| //java class public class GCD { public static int gcd( int num1, int num2) { if (num2 == 0 ) { return num1; } else { return gcd(num2, num1%num2); } } } //main class import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print( "Enter the base number: " ); int num1 = input.nextInt(); System.out.print( "Enter the power: " ); int num2 = input.nextInt(); GCD access = new GCD(); System.out.print( "The GCD of 2 numbers is: " + access.gcd(num1, num2)); } } |