0
0
CProgramBeginner · 2 min read

C Program to Print Number Pattern with Output and Explanation

You can print a number pattern in C using nested for loops; for example, use for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { printf("%d ", j); } printf("\n"); } to print numbers from 1 up to the current line number.
📋

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 row number. Use one loop to handle rows and a nested loop to print numbers in each row.
📐

Algorithm

1
Get the number of rows (n) from the user.
2
Start a loop from 1 to n for each row.
3
Inside this loop, start another loop from 1 to the current row number.
4
Print the current number in the inner loop.
5
After the inner loop ends, print a new line to move to the next row.
6
Repeat until all rows are printed.
💻

Code

c
#include <stdio.h>

int main() {
    int n;
    printf("Enter number of rows: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }
    return 0;
}
Output
Enter number of rows: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
🔍

Dry Run

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

1

Input

User enters n = 3

2

First outer loop iteration (i=1)

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

3

Print newline

Prints '\n' to move to next line

4

Second outer loop iteration (i=2)

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

5

Print newline

Prints '\n' to move to next line

6

Third outer loop iteration (i=3)

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

7

Print newline

Prints '\n' to finish pattern

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

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, each iteration representing one row of the pattern.

Step 2: Inner loop prints numbers

The inner for loop runs from 1 to the current row number, printing numbers in increasing order.

Step 3: New line after each row

After printing numbers for a row, printf("\n") moves the cursor to the next line for the next row.

🔄

Alternative Approaches

Reverse number pattern
c
#include <stdio.h>

int main() {
    int n;
    printf("Enter number of rows: ");
    scanf("%d", &n);

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }
    return 0;
}
Prints the pattern in reverse order, starting from n numbers down to 1.
Print same number in each row
c
#include <stdio.h>

int main() {
    int n;
    printf("Enter number of rows: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%d ", i);
        }
        printf("\n");
    }
    return 0;
}
Prints the row number repeatedly in each row instead of increasing numbers.

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

Time Complexity

The program uses nested loops: the outer loop runs n times and the inner loop runs up to n times, resulting in O(n^2) time.

Space Complexity

The program uses only a few variables and prints directly, so space complexity is O(1).

Which Approach is Fastest?

All approaches use nested loops with similar time complexity; differences are mainly in output style, not performance.

ApproachTimeSpaceBest For
Increasing number patternO(n^2)O(1)Simple ascending patterns
Reverse number patternO(n^2)O(1)Descending patterns
Same number per rowO(n^2)O(1)Patterns with repeated numbers
💡
Use nested loops where the outer loop controls rows and the inner loop controls columns to print patterns.
⚠️
Beginners often forget to print a newline after each row, causing all output to appear on one line.