Here is my code to create a program that takes a user input and lists multiples of 7 that relate to that number.
For example: The user inputs 3, I need the output to be "7, 14, 21".
Currently if I enter a number less than 7, the program complies without printing an output, but as soon as I enter 7 or any number higher than 7 then the program compiles and prints exactly what I need it to.
So the problem I need to fix is to be able to enter a number lower than 7 and recieve the correct output.
Thanks in advance!
import java.util.Scanner;
public class MultiplesOfSeven {
public static void main(String[] args){
int j = 0;
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(j = 1; j <= n; j++){
if(j % 7 == 0){
System.out.print(j + " ");
for (int counter = 0 ; counter < n ; counter++) {
System.out.print(j*(2 + counter) + " ");
}
}
}
}
Don't overthink the loop here. As alternatives, both which mean you can delegate the %
check, consider
for (j = 0; j < n; ++j){
// output (j + 1) * 7;
}
or, the less elegant due to your having to write 7
in three places
for (j = 7; j <= n * 7; j += 7){
// output j
}