First Multiple (break)
Completed
Read n numbers (first line n) and a divisor d on the last line. Print the first number divisible by d, or none.
Input
n, then n integers, then d.
Output
A number or none.
Example
Input
5 7 11 12 9 18 3
Output
12
Editor
Java
Hints
Read all numbers first, then search.
break leaves the loop immediately.
Solution explanation
Read the whole input before searching, so leaving the loop early never leaves input unread.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
int d = sc.nextInt();
String answer = "none";
for (int x : nums) {
if (x % d == 0) {
answer = String.valueOf(x);
break;
}
}
System.out.println(answer);
}
}