How do i create a backward triangle in java using for loops -
i need make triangle looks this
* ** *** **** ***** ****** ******* currently have working 1 looks
* ** *** **** ***** ****** ******* using loop :
public static void standard(int n) { for(int x = 1; x <= n; x++) { for(int c = 1; c <= x; c++) { system.out.print("*"); } system.out.println(); } } how go making work
* ** *** **** ***** ****** ******* here attempt:
public static void backward(int n) { for(int x = 7; x <= n; x++) { for(int y = 1; y >= x; y--) { if (x >= y) { system.out.print("*"); } else { system.out.print(""); } } system.out.println(); } }
on every line print n chars: if index c < n - x, print space, otherwise print asterisk:
for (int x = 1; x <= n; x++) { (int c = 0; c < n; c++) system.out.print(c < n - x ? ' ' : '*'); system.out.println(); } output (n = 6):
* ** *** **** ***** ******
Comments
Post a Comment