using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
{
if (i % 2 == 0)
Console.Write("0");
else
Console.Write("1");
}
Console.WriteLine();
}
}
}
}
Output
11111
0000
111
00
1
How It Works
1. Outer loop.i grows from 1 to 5 and decides both the parity and the start of the inner loop.
2. Parity.i % 2 == 0 means even → print 0; otherwise print 1.
3. Inner loop.j runs from i to rows, repeating that character rows - i + 1 times.
Example 2 — User Input
Read the row count and use a ternary for compact 1-or-0 logic.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j;
Console.Write("Enter rows: ");
if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
Console.Write(i % 2 == 0 ? "0" : "1");
Console.WriteLine();
}
}
}
}
A smaller fixed demo — same parity-and-shrink idea, easier to trace by hand.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
{
if (i % 2 == 0) Console.Write("0");
else Console.Write("1");
}
Console.WriteLine();
}
}
}
}
Output
111
00
1
How It Works
1. Same rules. Odd rows print 1; even rows print 0; length shrinks each time.
2. Quick check. Three rows end on a single 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
flip parity
Swap even/odd outputs
Printing 0 on odd rows flips the pattern to start with zeros. Keep even → 0, odd → 1.
wrong length
Use j = 1..i instead
That grows rows instead of shrinking them. Keep for (j = i; j <= rows; j++).
check j
Test j % 2 instead of i % 2
That alternates inside the row (10101). This pattern uses one character per entire row.
rows = 1
Single 1
Output is just 1 — one odd row of length 1.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
If/else (Examples 1, 3)
O(n²)
O(1)
Ternary form (Example 2)
O(n²)
O(1)
Total printed characters are n + (n-1) + … + 1 = n(n+1)/2, which is quadratic in n.
Remember
Key Takeaways
Rule: odd i prints 1; even i prints 0.
Length: row i has rows - i + 1 characters via j = i..rows.
Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
Next step: Program 41 prints square numbers in a pyramid pattern.
One line: for each row, pick 1 or 0 from i % 2 and repeat it while the row length shrinks.
Frequently Asked Questions
It checks i % 2. When i is even, the row prints 0; when i is odd, the row prints 1.
The inner loop runs from j = i to rows, printing rows - i + 1 characters per row — decreasing from rows down to 1.
Swap the if/else outputs, or invert the condition so odd rows print 0 and even rows print 1.
Program 39 rotates digits 1..rows per row. Program 40 prints only 1 or 0 per row based on parity, with shrinking row length.
Replace 5 with rows in the outer loop bound — see Example 2.
Use Console.Write(ch + " ") instead of Console.Write(ch).
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single 1 on one line.
🤔
Did you know?
Odd rows print 1, even rows print 0 — chosen with i % 2. Row i prints rows - i + 1 characters; total prints = n(n+1)/2.