forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise07_14.java
48 lines (40 loc) · 1.38 KB
/
Exercise07_14.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
40
41
42
43
44
45
46
47
package ch_07;
import java.util.Scanner;
/*7.14 (Computing gcd) Write a method that returns the gcd of an
* unspecified number of integers. The method header is specified as follows:
* public static int gcd(int... numbers)
* Write a test program that prompts the user to enter five numbers,
* invokes the method to find the gcd of these numbers, and displays the gcd.
*/
public class Exercise07_14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] numsToFind = new int[5];
System.out.println("Please enter 5 numbers now to find their GCD:");
for (int i = 0; i < numsToFind.length; i++) {
numsToFind[i] = input.nextInt();
}
System.out.println("The GCD of these 5 integers is " + gcd(numsToFind));
}
public static int gcd(int... numbers) {
int minNum = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minNum) {
minNum = numbers[i];
}
}
int gcd = 1;
boolean checkNum;
for (int i = 2; i < minNum; i++) {
checkNum = true;
for (int j = 0; j < numbers.length; j++) {
if (numbers[j] % i != 0)
checkNum = false;
}
if (checkNum) {
gcd = i;
}
}
return gcd;
}
}