Pattern with loops Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pattern with loops Java

How can I get this output: 11111 22000 33300 44440 55555

26th Jan 2019, 2:19 PM
Altina
Altina - avatar
2 Answers
+ 3
You could do it like this Make a for loop that (in this case) loops 5 times (for the length of the numbers) Than make an int count that counts the value of the numbers Then make 2 for loops within the main for loop that do the following: int counter starts at 0 empty string str for( loop 5 times ){ add 1 to counter make str empty for( loop counter times ){ add counter (as string) to str } for( loop 5 - counter times ){ add 0 as string to str } Print str }
26th Jan 2019, 2:32 PM
Roel
Roel - avatar
0
Thank you for your answer!!! Can I have it like this: int n=5; for( int i=1; i<=n; i++){ for( int j=1; j<=i; j++){ System.out.print("i");}} System.ou.println(" ");
26th Jan 2019, 3:34 PM
Altina
Altina - avatar