-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd.java
39 lines (35 loc) · 1.13 KB
/
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
30
31
32
33
34
35
36
37
38
39
import java.util.*;
public class gcd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
if (a % b == 0) {
System.out.println("Greatest common divisor is : " + b);
} else if (b % a == 0) {
System.out.println("Greatest common divisor is : " + a);
} else {
int gcd = 1;
if (a > b) {
for (int i = 1; i < a; i++) {
if (a % i == 0 && b % i == 0) {
gcd = gcd * i;
} else {
continue;
}
}
System.out.println("Greatest common divisor is : " + gcd);
} else {
for (int i = 1; i < b; i++) {
if (a % i == 0 && b % i == 0) {
gcd = gcd * i;
} else {
continue;
}
}
System.out.println("Greatest common divisor is : " + gcd);
}
}
sc.close();
}
}