Home› C Programming Language for Problem Solving› / Programs / Logic to print the given number pattern 12345 1234 123 12 1
🚀 Programming Example

Logic to print the given number pattern 12345 1234 123 12 1

4,642 Views
Beginner
Code Snippet
Write a C program to print the given number pattern using loop. How to print the given triangular number pattern using for loop in C programming. Logic to print the given number pattern using loop in C program.

💻 Program Code

/**
 * C program to print number pattern
 * www.rummanansari.com 
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    printf("Enter N: ");
    scanf("%d", &N);

    for(i=1; i<=N; i++)
    {
        // Logic to print numbers
        for(j=1; j<=N-i+1; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

🖥 Program Output

                                Enter N: 5
12345
1234
123
12
1
                            

📘 Explanation

C PROGRAMMING

Print a Reverse Decreasing Number Pattern in C

Learn how to use nested loops in C programming to generate a decreasing number pattern such as 12345, 1234, 123, 12, and 1.

Objective

In this program, the user enters a number N. The program prints numbers from 1 to N on the first line and decreases one number on each subsequent line. This example helps beginners understand nested loops and pattern generation techniques.

Pattern programs are one of the best ways to master loops and iteration logic in C programming.

Prerequisites

Before Learning This Program

  • Basic understanding of C programming syntax
  • Knowledge of variables and data types
  • Understanding of for loops
  • Basic input and output using scanf() and printf()
  • Understanding of nested loops

Program Code


#include 

int main()
{
    int i, j, N;

    printf("Enter N: ");
    scanf("%d", &N);

    for(i=1; i<=N; i++)
    {
        for(j=1; j<=N-i+1; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Sample Input


Enter N: 5

Output


12345
1234
123
12
1

How the Program Works

1

User Inputs N

The user enters the number of rows to print.

The value entered by the user is stored in the variable N.

2

Outer Loop Controls Rows

One iteration represents one row.

The outer loop runs from 1 to N and determines how many rows are displayed.

3

Inner Loop Prints Numbers

Creates the actual pattern.

The inner loop prints numbers from 1 to (N − i + 1). Therefore each row contains one less number than the previous row.

4

Move to Next Line

End of row processing.

The statement printf("\n"); moves the cursor to the next line after a row is completed.

Pattern Formula

ROWS
Total Numbers in Row = N - i + 1

Here:

  • N = Total rows entered by the user
  • i = Current row number

Loop Execution Table

Row (i) N - i + 1 Numbers Printed
1 5 12345
2 4 1234
3 3 123
4 2 12
5 1 1

Visual Representation


Row 1 -> 12345
Row 2 -> 1234
Row 3 -> 123
Row 4 -> 12
Row 5 -> 1

Simple Analogy

Imagine a staircase where each step becomes shorter than the previous one. Similarly, each new row prints one less number.

Complexity Analysis

Metric Complexity
Time Complexity O(N²)
Space Complexity O(1)

Key Learning Outcomes

After Completing This Program You Will Understand

  • How nested loops work
  • How rows and columns are controlled in pattern programming
  • How mathematical expressions can be used inside loop conditions
  • How to generate decreasing number patterns
  • How to build logic for advanced pattern problems

Key Takeaway

Pattern programs help developers build strong loop logic. This program demonstrates how an inner loop can dynamically reduce the number of values printed in each row using the formula N - i + 1.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.