-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise07_14.java
More file actions
52 lines (45 loc) · 1.44 KB
/
Copy pathExercise07_14.java
File metadata and controls
52 lines (45 loc) · 1.44 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
/***************************************************************************
* Calculate GCD from a list of integers. The methods support an unknown
* number of inputs while the main method only intakes 5 integers
****************************************************************************/
/**
*
* @author jorda
*/
public class Exercise07_14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter five integers: ");
int n1 = input.nextInt();
int n2 = input.nextInt();
int n3 = input.nextInt();
int n4 = input.nextInt();
int n5 = input.nextInt();
System.out.println("The GCD is " + gcd(n1, n2, n3, n4, n5));
}
//return the GCD of an unknow number of values
public static int gcd(int... numbers){
int gcd = 1;
boolean isDivisor;
for (int i = 2; i < min(numbers); i++) {
isDivisor = true;
for (int j: numbers){
if(j % i !=0)
isDivisor = false;
}
if(isDivisor)
gcd = i;
}
return gcd;
}
//take an unknow number of ints and return the smallest one
public static int min(int... numbers){
int min = numbers[0];
for (int j : numbers){
if(j < min)
min = j;
}
return min;
}
}