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("*"); } } } }
2 Réponses
+ 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+="*");
0
for (int l = 1; l <= 10; l++ ){
for (int i = 1; i <= l; i++){
System.out.print("*");
}
System.out.println();
}