How to Print 1 12 123 1234 12345 Number Pattern Program Using Loops in C, C++, Python, Java & PHP (2026)

Author:  Vishwas Kale
Reviewed by:  Mandira Singh
Nov 19, 2026
1 views | 7 min read

Number Pattern Program 1 12 123 1234 12345 in C, C++, Python, Java and PHP

Number Pattern Program 1 12 123 1234 12345 in C, Python, Java

Expected Output

1

12

123

1234

12345

Difficulty Level

Beginner-Level Program - Very Easy

Loop Details

Loop Count - 2

Loop Structure - Nested

Loops Available - for, while, do-while (Python does not have do-while)

Best Loop - for loop

Logic Explanation Step-by-Step Guide

  • We will print 5 rows, as shown in the expected output: 1, 12, 123, 1234, 12345.
  • This needs two nested loops.
  • The outer loop is controlled by variable (i), and the inner loop by variable (j).
  • The outer loop (i) starts from 1 and ends at 5. Each run prints one row.
  • The inner loop (j) starts from 1 and ends at the current value of (i), printing the value of (j) each time.
  • This is done using the condition (j) <= (i).
  • Once the inner loop (j) finishes, a new line is added so the next row starts on a new line.
  • The outer loop (i) then goes through the values 2, 3, 4, and 5.
  • When (i) goes past 5, the outer loop stops and the program ends.

Dry Run for Beginners: How the Loops Work Row by Row

Outer Loop (i) i<=5 Inner Loop (j) j<=i Output
Start i=1, 1st row j=1, print 1, new line 1st row = 1
i=2, 2nd row j=1,2, print 1,2, new line 2nd row = 1,2
i=3, 3rd row j=1,2,3 print 1,2,3, new line 3rd row = 1,2,3
i=4, 4th row j=1,2,3,4 print 1,2,3,4, new line 4th row = 1,2,3,4
i=5, 5th row j=1,2,3,4,5 print 1,2,3,4,5 new line 5th row = 1,2,3,4,5
i=6, (condition i <= 5 is false) Does not run, Outer loop stops Program Ends

Number Pattern Program (1, 12, 123, 1234, 12345) in Java

public class NumberPattern {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Number Pattern Program (1, 12, 123, 1234, 12345) in Python

for i in range(1, 6):
    for j in range(1, i + 1):
        print(j, end="")
    print()

Number Pattern Program (1, 12, 123, 1234, 12345) in PHP

<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $j;
    }
    echo "<br>";
}
?>
Note:- <br> is for browser output and for terminal use \n

Number Pattern Program (1, 12, 123, 1234, 12345) in JavaScript

for (let i = 1; i <= 5; i++) {
    let row = "";
    for (let j = 1; j <= i; j++) {
        row = row + j;
    }
    console.log(row);
}

Number Pattern Program (1, 12, 123, 1234, 12345) in C

#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d", j);
        }
        printf("\n");
    }
    return 0;
}

Number Pattern Program (1, 12, 123, 1234, 12345) in C++

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= i; j++) {
            cout << j;
        }
        cout << endl;
    }
    return 0;
}

Common Mistake

  • Many times forget to add a new line.
  • Frequently print outer loop variable (i) inside inner loop.
  • Making mistakes when comparing conditions.

Same Logic, Different Designs: Other Names of This Pattern

  • Left Triangle Number Pattern, Left-Aligned Number Pattern

    1
    12
    123
    1234
    12345

  • Right Triangle Number Pattern, Right-Aligned Number Pattern

            1
          12
        123
      1234
    12345

  • Centered Number Triangle Pattern, Number Triangle

             1
           1   2
         1   2   3
       1   2   3   4
     1  2   3   4   5 

About the Author

  • Vishwas Kale
  • Vishwas Kale

    ,  India
    QA Lead, 10+ years of hands-on experience in software testing, test automation, and quality assurance.

A passionate technical writer who enjoys turning complex software concepts into clear, engaging content. Curious about technology, testing, and innovation, with a love for learning and sharing ideas.

About the Reviewer

  • Mandira Singh
  • Mandira Singh

    ,  India
    QA Manager, 17+ years of experience leading QA teams, testing strategies, and software quality processes.

A technology enthusiast who enjoys exploring new ideas, following software trends, and learning about digital innovation. Outside work, enjoys reading, travel, and discovering new perspectives.

Coding Interview Programs