Member Avatar for Member #413109

Hi, I need help with this program here. I want to print an isosceles triangle. But, I keep ending up with this here where they are being spaced:
--------*
-------* *
------* * *
-----* * * *
----* * * * *
---* * * * * *
--* * * * * * *
-* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
Here is my code for it:

int num = 10;
		int i;
		int k;
		int j;
		
		for(i = 1; i <= num; i++) 
		{
			for(k = num; k >= i; k--)
			{
				System.out.print(" ");
			}
	
			for(j = 1; j <= i; j++) 
			{
				System.out.print("*");
				System.out.print(" ");
			}

			System.out.println();
		}

Even when I remove the "System.out.print(" ");" in the third for loop,

for(j = 1; j <= i; j++) 
			{
				System.out.print("*");
				System.out.print(" ");
			}

it would end up like this:
--------------*
-------------**
-----------***
----------****
--------*****
------******
-----*******
---********
--*********
**********
So can anyone help me on this? Thanks.
(Note: Sorry if I didn't draw the triangles correctly. This is my first time doing this)

Dani AI

Generated

Nice catch by on the 1-3-5 pattern for the no-spaces version. Since @RaigaX9 confirmed the assignment wants spaces between asterisks, the two keys are: (1) compute left padding from the base width, and (2) avoid a trailing space after the last asterisk on each line. Also watch the classic off-by-one in padding; the final row should have 0 leading spaces, not 1.

Here is a compact pattern that centers a spaced isosceles triangle cleanly and keeps the output monospaced-friendly. It builds each row once, trims the trailing gap, and uses exactly n - i leading spaces:

public static void printTriangle(int n) {
    for (int i = 1; i <= n; i++) {
        StringBuilder row = new StringBuilder(2 * i - 1);
        for (int k = 0; k < n - i; k++) row.append(' ');
        for (int s = 0; s < i; s++) {
            if (s > 0) row.append(' ');
            row.append('*');
        }
        System.out.println(row);
    }
}

If you ever need the solid (no spaces) isosceles version, switch to odd counts per row and keep the same padding idea: stars per row = 2*i - 1, left padding = n - i. That is exactly what hinted at with the 1-3-5-7-9 progression. A quick sanity check: top row width is 1, bottom row width is 2*n - 1, and the left padding decreases by one each line so the figure stays centered.

Recommended Answers

All 5 Replies

whats the problem? this code already prints an isosceles triangle?

whats the problem? this code already prints an isosceles triangle?

sorry... now I see that you are complaining about spaces...

your are incrementing the asteriks one by one in every line.. if you dont want spaces, then you should go with 1-3-5-7-9... asteriks on each line....

Member Avatar for Member #413109

sorry... now I see that you are complaining about spaces...

your are incrementing the asteriks one by one in every line.. if you dont want spaces, then you should go with 1-3-5-7-9... asterisk on each line....

Nevermind, I sort of did read the directions of the homework my professor has given us incorrectly. It's suppose to have spaces separating them and I sort of miss reading that part lol. In my professor's website, he forgot to put spaces in that triangle he wanted us to do and I just assumed that he wanted to make it without any spaces :p. Anyways, thanks for your help though and I did what you said and that works too. I apologize for this and again thanks. :)

Nevermind, I sort of did read the directions of the homework my professor has given us incorrectly. It's suppose to have spaces separating them and I sort of miss reading that part lol. In my professor's website, he forgot to put spaces in that triangle he wanted us to do and I just assumed that he wanted to make it without any spaces :p. Anyways, thanks for your help though and I did what you said and that works too. I apologize for this and again thanks. :)

oh teachers... :)

Member Avatar for Member #413109

oh teachers... :)

lol. Yeah, I know. Drove me crazy over the weekend. XD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.