-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCD.java
More file actions
35 lines (29 loc) · 724 Bytes
/
Copy pathGCD.java
File metadata and controls
35 lines (29 loc) · 724 Bytes
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
/**
* GCD
*/
public class GCD {
public static void main(String[] args) {
gcdForLoop(81, 253);
gcdWhileLoop(253, 81);
}
static void gcdForLoop(int a, int b){
int gcd = 1;
for(int i = 1; i < a && i < b; i++){
//Checks if i i is a factor of both integers
if(a % i == 0 && b % i == 0){
gcd = i;
}
}
System.out.printf("GCD of %d and %d is %d. ",a,b,gcd);
}
static void gcdWhileLoop(int a, int b){
while(a != b){
if(a > b){
a -=b;
}else{
b -= a;
}
}
System.out.printf("GCD While Loop, %d. ",a);
}
}