Is there a way i can write this code using any loop and still get the same output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there a way i can write this code using any loop and still get the same output?

public class Example{ public static void main(String []args){ System.out.println("1"); System.out.println("33"); System.out.println("555"); System.out.println("7777"); System.out.println("99999"); } }

12th Aug 2020, 6:22 AM
John
John - avatar
4 Answers
+ 11
public class Main { public static void main(String[] args) { for (int i=1;i<=5;i++) { System.out.println((Integer.toString((i*2)-1)).repeat(i)); } } }
12th Aug 2020, 9:14 AM
♪A ruba♪💖
♪A ruba♪💖 - avatar
+ 2
In python: for i in range(1,6): print(i*str(i*2-1))
12th Aug 2020, 6:38 AM
[][]
0
Thanks
12th Aug 2020, 10:24 AM
John
John - avatar
0
Am getting an error saying method repeat(int) is unidentified for type string
12th Aug 2020, 11:50 AM
John
John - avatar