Write a program that displays 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 ... 1 2 3 4 5 6 7 8 9 10 Use nested loops. The outer loop body should execute 10 times. Each time the inner loop is activated, it should display one line of numbers. (use print statement to display each number. Use println statement with no arguments to go to the next line.
What do you notice? I suggest you just try to make this program. Then run it. If it doesn't work, try changing it to see if you can make it better. Maybe you discover something interesting, play with it and let yourself get side tracked. Think about it. Clearly this is a solvable question they're asking, otherwise they wouldn't have asked it of you. Try to have fun just playing around until you get what you need to come out by tweaking it and running it over and over.
Okay, I figured it out using this code, but now my output's looking weird: I'll show you why. This is my code: public static void main(String[] args) { int i, j; for (i = 1; i <= 10; i++) { System.out.println(i); for (j = 1; j <= i; j++) { System.out.print(" " + j); } } } And this is my output: 12 1 23 1 2 34 1 2 3 45 1 2 3 4 56 1 2 3 4 5 67 1 2 3 4 5 6 78 1 2 3 4 5 6 7 89 1 2 3 4 5 6 7 8 910
Keep playing with it, you're getting there! =D
Figured it out! class C4h8 { public static void main(String[] args) { int i = 1, j; for (i = 1; i <= 10; i++) { System.out.println(" " + i); for (j = 1; j <= i; j++) { System.out.print(" " + j + " "); } } } } Thanks!
Join our real-time social learning platform and learn together with your friends!