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

Tags: ,



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!

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 + " ");
}


Source: stackoverflow