somebody help me out with the c# program code which prints the following output:
1
0 1
1 0 1
0 1 0 1
where the height of the triangle is taken as command line argument.
|
Newbie Member
|
|
| 4Jan2012,13:18 | #2 |
|
This will help you:
Code:
StringBuilder sb = new StringBuilder();
//height of triangle
int height = 5;
bool latestZeroWritten = false;
for (int i = 1; i <= height; i++)
{
//decides whether next line starts with 1
latestZeroWritten = !(i % 2 == 0);
for (int j = 1; j <= i; j++)
{
if (latestZeroWritten)
{
sb.Append("1 ");
latestZeroWritten = false;
}
else
{
sb.Append("0 ");
latestZeroWritten = true;
}
}
sb.Append(Environment.NewLine);
}
MessageBox.Show(sb.ToString());
|
