java - Pascal's triangle without using arrays (only loops) -
i trying output pascal's triangle asterisks (*). code:
public static void main(string [] arg) { int n=3; for(int i=0;i<n;i++) { for(int j=0;j<n-i;j++) { system.out.print(" "); } boolean b=true; for(int k=0;k<i*2+1;k++) { if(b) { system.out.print("*"); b=false; } else { system.out.print(" "); b=true; } } } system.out.println(" "); } }
i have rechecked several times , failed find error. let me know whether if-block implemented correctly. following code not giving required output below:
* * * * * *
your system.out.println()
statement outside of loop instead of inside.
(int = 0; < n; i++) { (int j = 0; j < n - i; j++) { system.out.print(" "); } boolean b = true; (int k = 0; k < * 2 + 1; k++) { if (b) { system.out.print("*"); b = false; } else { system.out.print(" "); b = true; } } } system.out.println(" "); // called once // output // * * * * * *
just move inside close brace , program work.
(int = 0; < n; i++) { (int j = 0; j < n - i; j++) { system.out.print(" "); } boolean b = true; (int k = 0; k < * 2 + 1; k++) { if (b) { system.out.print("*"); b = false; } else { system.out.print(" "); b = true; } } system.out.println(" "); // called once each iteration } // output // * // * * // * * *
Comments
Post a Comment