I need explanation for this output in using for loop and how it works... please anyone can teach me.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

I need explanation for this output in using for loop and how it works... please anyone can teach me..

1 12 123 1234 123 12 1

1st Mar 2020, 9:09 AM
DODDABASAPPA
DODDABASAPPA - avatar
3 Answers
+ 2
First thing you have to do, is recognise the patterns. In this case the simplest thing to do is split it in two parts, first try to do the first 4 lines. When you are supposed to repeat the same thing, you use a loop. So for printing each line, you will write a for loop where the variable goes from 1 to 4. Then inside this loop you will create another for loop that is supposed to print the numbers. In this one, the variable will always start from 1 and repeat as many times as the outer loop variable. Inside the inner loop you put your System.out.print statement. Try to do it on your own, based on this explanation it should be very easy. Post your attempt on the code playground, if you still have questions, and report back ;)
1st Mar 2020, 9:21 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Yea... thank you bro I got it output ☺️ public class Program { public static void main(String[] args) { for(int i = 1; i<=5; i++) { for(int j=1; j<=i; j++) { System.out.print(j); } System.out.println(""); } for(int i = 1; i<=4; i++) { for(int j=4; j>=i; j--) { System.out.print(j); } System.out.println(""); } } }
1st Mar 2020, 9:31 AM
DODDABASAPPA
DODDABASAPPA - avatar
0
DODDABASAPPA , but you get different pattern at the end 4321 432 43 4
1st Mar 2020, 9:21 PM
zemiak