Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.39/5 (7 votes)
See more:
i want to print this format in c# but not getting the result
the output i want is:-

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

could any one please help me...I've try my level best but the result is -ve...just send me the programming for this. I write this programming:-

C#
int number = 5;
            int i, j, k;
            for (i = 1; i <= number; i++)
            {
                for (j = 1; j <= number - i; j++)
                {
                    Console.Write("");
                }
                for (k = 1; k <= i; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine("");
            }
            for (i = 1; i <= number; i++)
            {
                for (j = 1; j <= number - i; j++)
                {
                    Console.Write(" ");
                }
                for (k = 1; k <= i; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine("");
            }

& it print this
*
**
***
****
*****
*
**
***
****
*****
please send me ASAP....thanks in advance.
Posted
Updated 21-Nov-17 7:13am

1 solution

Try this. This seems to be working,.

C#
int number = 5;

               for (int i = 0; i < number; ++i)
               {
                   for (int j = 0; j <= i; ++j)
                   {
                       Console.Write("*");
                   }

                   if (i != number - 1)
                   {
                       Console.Write(" ");
                   }
                   else
                   {
                       Console.Write(" * ");
                   }
                   for (int j = 0; j <= i; ++j)
                   {
                       Console.Write("*");
                   }

                   Console.WriteLine();
               }
 
Share this answer
 
Comments
Member 9901458 22-Apr-13 2:44am    
*****
* *
* *
* *
*****

Progarm for this?
Rahul Rajat Singh 22-Apr-13 4:42am    
What have you tried so far? these are homeworks and i bet you can do them easily by yourself.
Member 9901458 22-Apr-13 5:26am    
int number = 5;

for (int i = 0; i < number;i++)
{
if (i == 0 || i == 4)
{
for (int j = 0; j < number; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
if (i >= 1 && i <= 3)
{
for (int j = 0; j < number; j++)
{
if (j == 0 || j == 4)
{
Console.Write("*");
}
else if (j >= 1 && j <= 3)
{
Console.Write(" ");
}
}
Console.WriteLine();
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900