-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCD.java
More file actions
23 lines (22 loc) · 854 Bytes
/
GCD.java
File metadata and controls
23 lines (22 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;
public class GCD {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number: ");
int firstNumber = scanner.nextInt();
System.out.println("Enter second number: ");
int secondNumber = scanner.nextInt();
System.out.println(getGreatestCommonDivisor(firstNumber,secondNumber));
}
public static int getGreatestCommonDivisor(int firstNumber, int secondNumber) {
if(firstNumber>=10 && secondNumber>=10) {
int smallerNumber = Math.min(firstNumber, secondNumber);
for(int gcd = smallerNumber; gcd > 0; gcd--) {
if((firstNumber%gcd==0) && (secondNumber%gcd==0)) {
return gcd;
}
}
}
return -1;
}
}