Skip to content
Advertisement

Trying to output 0 1 2 3 8 5 6 14 8 9 using a for loop in Java [closed]

This is what I have so far:

for (int i = 0; i<= 9; i++){
            int output = i;
            if (i % 4 == 0){
                output *= 2;
            }
            System.out.print(output + " ");
        }

and it outputs this:

0 1 2 3 8 5 6 7 16 9

I’m not sure why it’s multiplying the 8 and not the 7 like I want it to do. It’s supposed to output this:

0 1 2 3 8 5 6 14 8 9

Thanks for any feedback!

Advertisement

Answer

Try it like this.

for (int i = 0; i<= 9; i++){
       int output = i;
       if (i == 4 || i ==7){  // <-- when to double the value       
                output *= 2;
        }
        System.out.print(output + " ");
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement