-5

Im trying to make a table that prints numbers from 0 to 255 in decimal format. The print out should be 16 numbers per line using printf and %5d format. I made a code but idk how to make it enter after every 16 numbers.

    for(int x = 0; x <255;x++) {
         
    System.out.printf("%5d\n",x);

But it prints out in a line lol.

3

2 Answers 2

1

you can add a simpel condition

 for(int x = 0,y=0; x <255;x++,y++){
            if(y==16)
            {
                System.out.printf("\n");
                y=0;
            }   
            System.out.printf("%5d",x);
                
        }

so what's happening here is that i added y veriable that's increments, every time y is equal to 16 it's entering the if condition,there we printing a new line and to make it so every 16 numbers the program will print a new line im returning y to 0.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! Can you explain what you did though by adding the y=0? How does it know to make 16 numbers per line?
0

try to add if statement:

if (i % 17 == 0) {System.out.Println("");

basically, every time the index equals a number that is divided by 17, it goes to the next line. And you may want to get rid of "\n" since it skips to the next line in every iteration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.