-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactors.java
More file actions
30 lines (27 loc) · 1.04 KB
/
Factors.java
File metadata and controls
30 lines (27 loc) · 1.04 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
//Exercise 5.7: Write a method named areFactors that takes an integer n and an array of integers, and returns true if the numbers in the array are all factors of n (which is to say that n is divisible by all of them).
import java.util.Scanner;
import java.util.Arrays;
public class Factors {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer:");
int n = in.nextInt();
System.out.println("Enter the no. elements you want in the array:");
int k = in.nextInt();
System.out.println("Enter all the elements:");
int[] numbers = new int[k];
for (int i = 0; i < k; i++) {
numbers[i] = in.nextInt();
}
System.out.println(areFactors(n, numbers));
}
public static boolean areFactors(int n, int[] a) {
boolean bool = true;
for (int number: a) {
if (n % number != 0) {
bool = false;
}
}
return bool;
}
}