<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&family=DM+Mono:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&display=swap" rel="stylesheet"/>

Star Pattern Series 1 — Triangle Pyramid in C

By Mihir Pipermitwala · · 4 min read

⚡ TL;DR

Print the right angle triangle (half pyramid) star pattern in C using nested for loops. Full source code with output, explanation, and complexity analysis.

In this program, we will see the right angle triangle pyramid pattern (also called the half triangle pattern) in the C programming language using stars. Each row contains one more star than the previous row, growing from 1 star up to n stars.

Pattern to Print

Copy
*
**
***
****
*****

Implementation

The C program for the above right angle triangle pyramid pattern is as follows:

Copy
#include<stdio.h>

int main() {
    int i, j, noRows;
    printf("Enter number of rows for pattern = ");
    scanf("%d",&noRows);
    for(i=0; i< noRows; i++) {
        for(j=0;j<=i;j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output

Copy
Enter number of rows for pattern = 5

*
**
***
****
*****

How It Works

Two nested for loops do all the work:

  1. Outer loop (i) — runs once per row, from 0 to noRows - 1. It decides which row we are printing.
  2. Inner loop (j) — runs i + 1 times on row i (because of j <= i), printing exactly one star per iteration. Row 0 prints 1 star, row 1 prints 2, and so on.
  3. Newline — after the inner loop finishes a row, printf("\n") moves the cursor to the next line.

The key relationship is j <= i: the column count is tied directly to the current row number, which is what produces the growing triangle shape.

Complexity

  • Time: O(n²) — total stars printed = 1 + 2 + … + n = n(n+1)/2
  • Space: O(1) — only loop counters are used

Practice Variations

  1. Print the inverted version of this pattern (5 stars down to 1) — see Star Pattern Series 2
  2. Replace * with row numbers so each row prints its row number repeatedly
  3. Add leading spaces to convert the half pyramid into a centered full pyramid
  4. Rewrite the same pattern using while loops instead of for loops

Happy coding!

References

FAQ

How do I print a right triangle star pattern in C?

Use two nested for loops: the outer loop runs once per row from 0 to n-1, and the inner loop prints i+1 stars using the condition j <= i, so row 0 prints one star and each later row prints one more. A newline printed after the inner loop moves output to the next row.

What is the logic behind the half pyramid pattern in C?

The logic is tying the column count directly to the row number. On row i, the inner loop runs i+1 times, printing one star per iteration, which makes the triangle grow from a single star to n stars. This j <= i relationship is the core idea behind almost every pyramid pattern variation.

What is the time complexity of the triangle star pattern program?

The time complexity is O(n²) because the total number of stars printed is 1 + 2 + … + n, which equals n(n+1)/2 — a quadratic number of print operations. Space complexity is O(1), since the program stores only the two loop counters regardless of how many rows the user requests.

What are some variations of the star triangle pattern in C?

Common variations include the inverted triangle that shrinks from n stars to one, a centered full pyramid made by adding leading spaces, replacing stars with repeated row numbers, and combining two triangles into a diamond shape. You can also rewrite the same output using while loops instead of for loops to practice loop control.

Mihir Pipermitwala

Written by Mihir Pipermitwala

Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.

More about Mihir →
Scroll down to load comments...
Related Blogs
Go JSON v2: A Practical Migration Guide to encoding/json/v2

Go JSON v2: A Practical Migration Guide to encoding/json/v2

Migrate a Go REST API to encoding/json/v2 in 7 steps: struct tags, omitempty, unknown fields, error handling, and jsontext streaming, plus a v1 vs v2 comparison.

GOTUTORIALDEVELOPER TOOLS

November 12, 2026

Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlock Flutter's potential with clean code. Learn best practices for writing maintainable Dart code, with examples and essential principles.

CLEAN CODECODE BEST PRACTICESCODE MAINTAINABILITYCODE ORGANIZATIONCODE READABILITY

June 02, 2023

Top 9 Local Databases for Flutter: Full Comparison

Top 9 Local Databases for Flutter: Full Comparison

Compare the best local databases for Flutter (Isar, Hive, Sqflite, ObjectBox, Realm, Drift, Floor, CBL, Sembast). Find pros, cons, and performance comparisons.

CODINGFLUTTERFLUTTER LOCAL DATABASESLEARN TO CODENOSQL

April 06, 2023

Related Tutorials
Essential Git Skills: How to Delete Remote Branches

Essential Git Skills: How to Delete Remote Branches

Keep your repository clean and organized. This guide walks you through how to delete both local and remote branches in Git, step by step.

BITBUCKETDELETE LOCAL BRANCHDELETE REMOTE BRANCHGITGIT BRANCH CLEANUP AUTOMATION

June 12, 2024

Show an Offline Message for No Internet in Flutter

Show an Offline Message for No Internet in Flutter

You have to use the 'Connectivity Flutter Package' to achieve this feature on your App. This package helps to know whether your device is online or offline.

CONNECTIVITY PLUSDEPENDENCIESFLUTTERFLUTTER DEVELOPMENTFLUTTER PACKAGES

April 09, 2024

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Implement TabBar and TabBarView in Flutter step by step — customize tab indicators, enable scrollable tabs, and change tabs programmatically.

CODINGDEFAULT TAB CONTROLLERFLUTTERLEARN TO CODETAB CONTROLLER

July 26, 2023

File Manipulation in Flutter: Best Practices & Examples

File Manipulation in Flutter: Best Practices & Examples

Master Flutter file manipulation — permission management, directory handling, and practical read-write scenarios to elevate your app's file handling.

CODINGFLUTTERFLUTTER DEVELOPMENTFLUTTER FILE OPERATIONFLUTTER PATH PROVIDER

June 30, 2023

Related Recommended Services
Visual Studio Code for the Web

Visual Studio Code for the Web

Build with Visual Studio Code, anywhere, anytime, in your browser.

IDEVISUAL STUDIOVISUAL STUDIO CODEWEB
Renovate | Automated Dependency Updates

Renovate | Automated Dependency Updates

Renovate Bot keeps source code dependencies up-to-date using automated Pull Requests.

AUTOMATED DEPENDENCY UPDATESBUNDLERCOMPOSERGITHUBGO MODULES
Best XML Formatter and XML Beautifier

Best XML Formatter and XML Beautifier

Online XML Formatter will format xml data, helps to validate, and works as XML Converter. Save and Share XML.

XMLXML BEAUTIFIERXML CONVERTERXML FORMATXML FORMATTER
Kubecost | Kubernetes cost monitoring and management

Kubecost | Kubernetes cost monitoring and management

Kubecost started in early 2019 as an open-source tool to give developers visibility into Kubernetes spend. We maintain a deep commitment to building and supporting dedicated solutions for the open source community.

CLOUDKUBECOSTKUBERNETESOPEN SOURCESELF HOSTED
Related Recommended Stories
How GitHub reduced testing time for iOS apps with new runner features

How GitHub reduced testing time for iOS apps with new runner features

Learn how GitHub used macOS and Apple Silicon runners for GitHub Actions to build, test, and deploy our iOS app faster.

IOSGITHUBTESTINGRUNNER
5 ways to transform your workflow using GitHub Copilot and MCP

5 ways to transform your workflow using GitHub Copilot and MCP

Learn how to streamline your development workflow with five different MCP use cases.

AGENT MODECODING AGENTCOPILOTFIGMAGITHUB
One weird trick for powerful Git aliases

One weird trick for powerful Git aliases

Advanced Git Aliases

ALIASALIAS TEMPLATEATLASSIANBITBUCKETGIT
Awesome Python

Awesome Python

An opinionated list of awesome Python frameworks, libraries, software and resources

AWESOMEAWESOME PYTHONCOLLECTIONSGITHUBPYTHON
Related Recommended Tools
Find out what websites are built with - Wappalyzer

Find out what websites are built with - Wappalyzer

Find out the technology stack of any website. Create lists of websites and contacts by the technologies they use.

ADD ONSANALYTICSAPP STOREAPPLEBOOKING
Sourcetree | Free Git GUI for Mac and Windows

Sourcetree | Free Git GUI for Mac and Windows

A Git GUI that offers a visual representation of your repositories. Sourcetree is a free Git client for Windows and Mac.

GITGITHUBGITLABATLASSIANBITBUCKET
Related Recommended Videos