i need out put like this in c#

Posted by Johnseelan under C# on 9/21/2012 | Points: 10 | Views : 820 | Status : [Member] | Replies : 3
1
2 3
4 5 6
7 8 9 10

and
*
* *
* * *
* *
*
we choose c#.




Responses

Posted by: Ranjeet_8 on: 9/21/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down

int k = 1;
for (int i = 1; i < 5; i++)
{
for (int j = 1; j <= i; j++)
{
Response.Write(k);
k += 1;
}
Response.Write("</br>");
}

output will be:
1
2 3
4 5 6
7 8 9 10
 

for (int i = 1; i < 5; i++)
{
for (int j = 1; j <= i; j++)
{
Response.Write("*");
}
Response.Write("</br>");
}
for (int x = 4; x >1; x--)
{
for (int y = 1; y < x; y++)
{
Response.Write("*");
}
Response.Write("</br>");
}

Out put will be :
*
* *
* * *
* * * *
* * *
* *
*


Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pgayath on: 9/21/2012 [Member] Starter | Points: 25

Up
0
Down
hi Johnseelan:

The below code works perfectly for this structure. Please modify for other structure if you need.
1
2 3
4 5 6
7 8 9 10

int count = 1;

int i = 1;
string strOutput=null;

do
{
switch (count)
{
case 1:
strOutput = i.ToString();
count = 2;
i++;
break;

case 2:
strOutput += "\r\n" + i.ToString() + "\t" + (i + 1).ToString();
count = 3;
i = i + 2;
break;

case 3:
strOutput += "\r\n" + i.ToString() + "\t" + (i + 1).ToString() + "\t" + (i + 2).ToString();
count = 4;
i = i + 3;
break;

case 4:
strOutput += "\r\n" + i.ToString() + "\t" + (i + 1).ToString() + "\t" + (i + 2).ToString() + "\t" + (i + 3).ToString();
count = 5;
i = i + 4;
break;

default:
break;
}

} while (i <= 10);
MessageBox.Show(strOutput);


Thanks and Regards,
Gayathri P

Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pgayath on: 9/21/2012 [Member] Starter | Points: 25

Up
0
Down
Good one Ranjeet

Johnseelan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response