Can you improve this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you improve this code?

public class ForLoop0 { /* This code generates a triangle using asterisks */ public static void main(String[] args) { // First for loop makes 10 lines. for (int l = 1; l <= 10; l++ ){ System.out.println(); // Second for loop prints asterisks. for (int i = 1; i <= l; i++){ System.out.print("*"); } } } }

3rd Apr 2019, 8:42 PM
Anava
Anava - avatar
2 Answers
+ 23
● number of stars in each row forming an A.P, so you can add 1 star each time to form next row : String str=""; for(int i=1;i<=10;i++) System.out.println(str+="*");
4th Apr 2019, 2:39 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
0
for (int l = 1; l <= 10; l++ ){ for (int i = 1; i <= l; i++){ System.out.print("*"); } System.out.println(); }
3rd Apr 2019, 9:02 PM
JavaBobbo
JavaBobbo - avatar