Guys can you help me out to turn the simple program into a single loop. I want to solve the series 1+22+333+4444+55555 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Guys can you help me out to turn the simple program into a single loop. I want to solve the series 1+22+333+4444+55555

class Patterns { public static void main(String args[]) { int s=0; for(int i =1;i<=5;i++) {int d=0; for(int j=1;j<=i;j++) { d=d*10+i; } s=s+d; } System.out.println(s); } }

26th May 2020, 2:26 PM
Student 31
Student 31 - avatar
7 Answers
+ 1
I think you only need one loop here and a series of int -> String -> int conversions. One iterator will be enough for this.
26th May 2020, 2:33 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
I cannot use single loop to get the proper output
26th May 2020, 2:27 PM
Student 31
Student 31 - avatar
0
Is there any way to turn 2 loops into 1 loop
26th May 2020, 2:28 PM
Student 31
Student 31 - avatar
0
Cant understand
26th May 2020, 2:33 PM
Student 31
Student 31 - avatar
0
We have to extract all the numbers
26th May 2020, 2:34 PM
Student 31
Student 31 - avatar
0
Student 31 Yes, but each of them is dependent on one variable and is equal to: int(i) * str(i), right? So you only need one iterable.
26th May 2020, 2:36 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
Thanks for helping Kuba Siekierzynski & Mireille
26th May 2020, 2:39 PM
Student 31
Student 31 - avatar