views:

46

answers:

3

Hi

I have this shape and I want to output it to ConoleApplication Windows.

alt text

I have this code but it doesn't work as I need :

int i,j;

for(i=0;i<5;i++)

{

for(j=5-i;j>0;j--)

Console.WriteLine(" ");

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

Console.WriteLine("*");

Console.WriteLine("\n");

}

Thanks in advance.

EDIT : I am very sorry

+1  A: 

Something like:

            for (j = 0; j <= 2 * i; j += 2)
            {
                printf("*");
                printf(" ");
                // or Console.Write("* ") if we are talking C#
            }

which writes the spaces between asterisks (plus a spare one; you could remove that if it is important).

Marc Gravell
Why did not you use `printf("* ")` once..?
Betamoo
@Betamoo - simple; I rarely (if ever) use C, and I couldn't be 100% sure that it would work, but the question itself showed usage for the versions shown.
Marc Gravell
A: 

You just used WriteLine instead of Write

Here is the correct code:

    int i, j;

    for (i = 0; i < 5; i++)
    {

        for (j = 5 - i; j > 0; j--)
            Console.Write(" ");

        for (j = 0; j <= 2 * i; j++)
            Console.Write("*");

        Console.WriteLine();

    }
Betamoo
Isn't that lacking the spaces between the stars?
dtb
I think that is not what OP wanted to do... if so, he would try to print a space in his code...
Betamoo
Just trying to find what is wrong with his code... not re-inventing a new one!!
Betamoo
+1  A: 
dtb