Star Pyramid 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 s=1;s<=n-i;s++) printf(" ");
        for(int j=1;j<=2*i-1;j++) printf("*");
        printf("\n");
    }
}

C Output

Input:  
4  

Output:  
   *
  ***
 *****
*******


C++ Program

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

C++ Output

Input:  
5  
Output: * *** ***** ******* *********


JAVA Program

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

JAVA Output

Input:  
3  

Output:  
  *
 ***
*****


Python Program

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

Python Output

Input:  
6  

Output:  
     *
    ***
   *****
  *******
 *********
***********


Deep-Dive, Extended Explanation
Recognizing the Pattern
A star pyramid is an old programming problem where row i has an equal number of symmetric arrangements of stars (*) and spaces for alignment. The formula is that row i has (2*i - 1) stars, and the spaces preceding the first star in row i are (n - i) spaces. This leaves us with a perfectly aligned triangle of stars.

Example
For n = 4:

Row 1 → Spaces: 3, Stars: 1 → " *"

Row 2 → Spaces: 2, Stars: 3 → " ***"

Row 3 → Spaces: 1, Stars: 5 → " *****"

Row 4 → Spaces: 0, Stars: 7 → "*******"

The spaces move one step to the left each row, and the stars move outward.

Real-Life Analogy
Consider a Christmas tree: the trunk is in the middle, and each new row you add becomes wider while remaining balanced. The same happens with each row of stars in a star pyramid: each row of stars becomes wider but remains centered.

Why It Matters
Star patterns illustrate the basic principle of nested loops and alignment logic. It makes you work with two distinct print operations per row:

Printing spaces to nudge stars into place.

Printing stars for the visible form.

The idea of symmetry is used in UI layouts, ASCII art, and center printing in console applications.

Learning Insights
Nested Loops: One loop manages the row, and two inner loops print spaces and stars individually.

Mathematical Pattern: Stars number in an odd series (1, 3, 5, 7…).

Separation of Concerns: Separating printing spaces and stars makes the code easier to understand.

In Interviews
Interviewers tend to challenge pyramid patterns to verify:

Loop control understanding

Maneuvering around various counters

Logic for output alignment
They might request you to convert this into an inverted pyramid, diamond, or stars with numbers.

SEO-Friendly Conclusion
The star pyramid algorithm is among the most common pattern questions in programming interviews, learning how to employ nested loops, align, and produce symmetrical outputs in C, C++, Java, and Python. By solving this problem, you have a good understanding for tackling complex pattern printing problems, console graphics, and text-based UI styling. To know how to compute spaces and stars per line is an applied skill relevant in competitive programming, UI layout design, and ASCII art creation.