forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCD.java
29 lines (23 loc) · 844 Bytes
/
GCD.java
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
/**
* Java program to find Greatest Common Divisor or GCD of
* two numbers using Euclid’s method.*/
import java.util.*;
public class GCD{
public static void main(String args[]){
//Enter two number whose GCD needs to be calculated.
Scanner sc = new Scanner(System.in);
System.out.println("Please enter first number to find GCD");
int num1 = sc.nextInt();
System.out.println("Please enter second number to find GCD");
int num2 = sc.nextInt();
System.out.println("GCD of two numbers " + num1 +" and "
+ num2 +" is :" + GCD(num1,num2));
}
private static int GCD(int num1, int num2) {
//base case
if(num2 == 0){
return num1;
}
return GCD(num2, num1%num2);
}
}