pls help me . . .
give me some hints on how to do this program . . .
a program that displays a mirrored-right triangle using asterisk. the program should ask the user to enter the number of rows and displays the mirrored-right triangle. the minimum is 3. there is a space separating the two right triangles. for example, 5

*         *
**       **
***     ***
****   ****
***** *****

im not good with looping structure. . .
pls help . . .
give me a clue on how to do this one pls

Edited by nix_xin: n/a

6 Years
Discussion Span
Last Post by Xufyan

use nested for loop to print, input number of rows from command line argument !

for (int i=0; i<5; i++){ // '5' is number of rows !!!
  for (int j=0; j<i+1; j++){
	System.out.print ("*");

	System.out.println ("");

Edited by Xufyan: n/a


you'll have to use three nested loops in a loop!

first loop to print the left pattern
2nd reverse loop for the decrement in number of spaces !
3rd loop to print the second pattern..!

thats all :)

Edited by Xufyan: n/a


nix nix, it is seen from the output shown in your post that

*        *
**      **
***    ***
****  ****

at the 1 st row: 1 star, 8 spaces, and 1 star are printed
at the 2 nd row: 2 stars, 6 spaces, and 2 stars are printed
at the 3 rd row: 3 stars, 4 spaces, and 3 stars are printed
at the 4 th row: 4 stars, 2 spaces, and 4 stars are printed
at the 5 th row: 5 stars, 0 space, and 5 starts are printed
This observation might be summarized in terms of a for loop body where i is the control variable starting by 1 up to 5 with each increment of one as follows.
at the i row: i stars, (10-2*i) spaces, and i stars printed.
Hence, the body of the “for loop” summarized above is thus written as follows:

	for (j=0;j<i;j++)  // to print i start(s) 
	for (j=0;j<10-2*i;j++) // to print (10-2*i) spaces
	System.out.print(" ");
	for (j=0;j<i;j++)  // to print i start(s)
	System.out.println(); // change to the next new line

nix_xni, you may also study Xufyan's suggestion as a reference to complete the coding.

Edited by tong1: n/a


This is exactly what I said, except your way of decrement in the number of Spaces !! :)
but in your program, at the 5th row, all spaces has been removed, but according to the output shown by nix xin one space should remain in the last row as well (5th row)

Have a look !

*        *
**      **
***    ***
****  ****

@nix xin,

Try this ,it is much easier for newbie like me and you! :D

for (int i=0; i<5; i++){ // number of Rows 0,1,2,3,4
	for (int j=0; j<i+1; j++) // print left side pattern
		System.out.print ("*");
	for (int j=6; j>i+1; j--) // print number of spaces
		System.out.print ("  ");
	for (int j=0; j<i+1; j++) // print right side pattern
		System.out.print ("*"); 
	System.out.println (" "); // change to the next line after printing each row !


*          *
**        **
***      ***
****    ****
*****  *****

if you want to use tong1's suggestion then change the second loop inside tong1's coding to

for (j=0;j<10-2*i+1;j++)

and start the loop i (rows loop) from i=1 not i=0

Edited by Xufyan: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.