- 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
3 Antworten
+ 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 ;)
+ 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("");
}
}
}
0
DODDABASAPPA , but you get different pattern at the end
4321
432
43
4