0
0
CsharpProgramBeginner · 2 min read

C# Program to Print Number Pattern

You can print a number pattern in C# using nested for loops like for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { Console.Write(j + " "); } Console.WriteLine(); } to print numbers in increasing order per line.
📋

Examples

Input3
Output1 1 2 1 2 3
Input5
Output1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Input1
Output1
🧠

How to Think About It

To print a number pattern, think of rows and columns. For each row from 1 to n, print numbers starting from 1 up to the current row number. Use one loop for rows and a nested loop for printing numbers in each row.
📐

Algorithm

1
Get the number of rows n from the user
2
For each row i from 1 to n, do:
3
For each number j from 1 to i, print j followed by a space
4
After inner loop ends, print a new line
5
End
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                Console.Write(j + " ");
            }
            Console.WriteLine();
        }
    }
}
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
🔍

Dry Run

Let's trace the program for n=3 to see how it prints the pattern.

1

Start outer loop with i=1

Inner loop runs j=1 to 1, prints '1 '

2

Print newline after inner loop

Output so far: 1

3

Outer loop i=2

Inner loop j=1 to 2, prints '1 2 '

4

Print newline

Output so far: 1 1 2

5

Outer loop i=3

Inner loop j=1 to 3, prints '1 2 3 '

6

Print newline

Final output: 1 1 2 1 2 3

i (row)j (column)Printed Numbers
111
21-21 2
31-31 2 3
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, each iteration represents a new line in the pattern.

Step 2: Inner loop prints numbers

The inner for loop prints numbers from 1 up to the current row number, creating the pattern.

Step 3: New line after each row

After printing numbers for a row, Console.WriteLine() moves to the next line for the next row.

🔄

Alternative Approaches

Using while loops
csharp
using System;
class Program {
    static void Main() {
        int n = 5;
        int i = 1;
        while (i <= n) {
            int j = 1;
            while (j <= i) {
                Console.Write(j + " ");
                j++;
            }
            Console.WriteLine();
            i++;
        }
    }
}
While loops achieve the same result but are less concise than for loops for counting.
Printing pattern in reverse order
csharp
using System;
class Program {
    static void Main() {
        int n = 5;
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                Console.Write(j + " ");
            }
            Console.WriteLine();
        }
    }
}
This prints the pattern starting from the largest row down to 1, reversing the pattern.

Complexity: O(n^2) time, O(1) space

Time Complexity

The nested loops run approximately n*(n+1)/2 times, which is O(n^2), because for each row i, the inner loop runs i times.

Space Complexity

The program uses constant extra space, only variables for counters, so O(1) space.

Which Approach is Fastest?

Using for loops is generally faster and clearer than while loops for counting patterns; reversing the pattern changes output but not complexity.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simple and clear pattern printing
Nested while loopsO(n^2)O(1)Same complexity, less concise
Reverse pattern with for loopsO(n^2)O(1)Printing pattern in reverse order
💡
Use nested loops where the outer loop controls rows and the inner loop controls columns for patterns.
⚠️
Beginners often forget to print a new line after each row, causing all output to appear on one line.