Half Pyramid Using Numbers in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main(){
    int n=4;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++) printf("%d ",j);
        printf("\n");
    }
}

C Output

Input:  
n = 4  

Output:  
1 
1 2 
1 2 3 
1 2 3 4 


C++ Program

#include <iostream>
using namespace std;
int main(){
    int n=5;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++) cout<<j<<" ";
        cout<<"\n";
    }
}

C++ Output

Input:  
n = 5 

Output:  
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 


JAVA Program

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

JAVA Output

Input:  
n = 3  

Output:  
1 
1 2 
1 2 3 


Python Program

n=6
for i in range(1,n+1):
    print(*range(1,i+1))

Python Output

Input:  
n = 6 

Output:  
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6


Deep-Dive, Extended Explanation
Understanding the Problem
The "Half Pyramid Using Numbers" is an easy yet effective exercise in mastering nested loops and pattern generation in programming. In this pattern, we display numbers in the shape of a triangle where each row begins from 1 and increases up to the row number that we are on. The elegance of this pattern lies in its simplicity of logic and structure, which makes it an excellent stepping stone for beginners to understand how loops can be combined.

When you look at the output:

1  
1 2  
1 2 3  
1 2 3 4
your brain immediately recognizes a pattern — each line includes one more number than the last line. This insight is the basis for coding.

Breaking Down the Logic
We do this with two loops:

The outer loop tells us how many rows (how tall we want the pyramid to be).

The inner loop controls how much to print on each row.

The inner loop iterates from 1 to the row number we are on in the outer loop.

This produces an increasing sequence row by row.

After printing a complete row, we advance to the next line so that the next sequence of numbers will be on a new row.

For instance, for n = 4:

Outer loop iterates 4 times.

In the 1st row, inner loop executes 1 time → prints 1

In the 2nd row, inner loop executes 2 times → prints 1 2

In the 3rd row, inner loop executes 3 times → prints 1 2 3

In the 4th row, inner loop executes 4 times → prints 1 2 3 4

Learning Insight
This code is not only about printing numbers. It illustrates:

Dependency between loops: The range of the inner loop is dependent on the outer loop's counter.

Flow control in nested loops: Control flow between loops and how variables are updated.

Pattern recognition: Observing a recurring pattern in real life and converting it into code.

When novices learn this logic, they can begin altering the pattern effortlessly:

Reverse numbers' order.

Substitute numbers with letters.

Display the pyramid right-aligned.

Develop hollow patterns.

Example
Suppose n = 5, then the output will be:


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
In this growth, each row has one number more than the last.

Real-Life Analogy
Imagine placing chairs in a classroom:

On day 1, you put out 1 chair.

On day 2, you put out 2 chairs in the next row.

On day 3, you put out 3 chairs in the next row.
By the time you reach the end, you have a triangle pattern of chairs. The manner in which you continue adding chairs in a specific sequence is similar to the way the inner loop functions in this program.

Why It Matters
You may consider this to be a beginner pattern problem, but the truth is that nested loops constitute the foundation of most significant programming activities:

Processing 2D arrays or matrices.

Generating tables and formatted reports.

Image processing (pixels are computed on a grid, just like rows and columns).

Building board games in which each cell within a grid gets refreshed.

After you know how to think through the pyramid logic, you can generalize to more difficult problems such as Sudoku solvers, pathfinding algorithms, and data visualization tools.

Programming Wisdom
In job interviews, small problems such as this one are frequently employed to evaluate:

How you break down a pattern.

How quickly you can turn observation into code.

How effectively you optimize a small chunk of logic without over-complicating it.

Most candidates dive straight into coding without considering how loops relate to each other, which results in errors. This exercise conditions you to think first before coding.

SEO-Optimized Conclusion
The half pyramid number pattern is also one of the most widely used beginner programming problems since it reinforces the understanding of nested loops, generation of sequences, and conditional formatting. Whether you’re learning C, C++, Java, or Python, mastering this simple pattern lays the groundwork for more advanced pattern printing challenges like inverted pyramids, Pascal’s triangle, and diamond shapes. By practicing this logic, you’ll gain the skills needed for solving array problems, matrix operations, and even algorithmic challenges that appear in coding interviews and real-world applications.