forked from nishitpanchal395/projecthactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCDCalculator.java
24 lines (21 loc) · 884 Bytes
/
GCDCalculator.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
package algorithms.recursion;
import java.util.Scanner;
public class GCDCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please input two integers: ");
int firstNumber = scanner.nextInt();
int secondNumber = scanner.nextInt();
scanner.close();
var maximum = Math.max(firstNumber, secondNumber);
var minimum = Integer.sum(firstNumber, secondNumber) - maximum;
System.out.println("GCD(" + maximum + ", " + minimum + ") = " + calculateGCD(maximum, minimum));
}
private static int calculateGCD(int firstNumber, int secondNumber) {
int remainder = firstNumber % secondNumber;
if (remainder == 0) {
return secondNumber;
}
return calculateGCD(secondNumber, remainder);
}
}