C (programming language)

C Program To Print Pyramid Star Pattern

The classic pyramid star pattern is a delightful visual representation of numbers, often encountered in coding challenges and introductory programming exercises. This article delves into creating a C program that prints this captivating pattern, exploring different approaches and delving into the underlying logic.

Understanding the Pyramid Star Pattern

The pyramid star pattern consists of rows of stars, arranged in a triangular shape, with the number of stars increasing in each subsequent row. Imagine a pyramid built with stars instead of bricks! Here’s an example of a 5-row pyramid star pattern:

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

The key to generating this pattern lies in understanding the relationship between the row number and the number of stars printed in that row.

Building the Logic: Loops and Conditions

To achieve this pattern, we leverage the power of loops and conditional statements in C. Here’s a breakdown of the logic:

  1. Outer Loop: We use an outer loop to iterate through the desired number of rows (usually determined by user input or a fixed value).
  2. Inner Loop: Within the outer loop, we nest another loop to control the number of stars printed in each row. The number of iterations in this inner loop should correspond to the current row number.
  3. Star Printing: Inside the inner loop, we simply print a star symbol (“*”) for each iteration.
  4. Conditional Spacing: Optionally, to enhance the pyramid shape, we can add spaces before the stars in each row. The number of spaces typically decreases as we move down the pyramid, emphasizing the triangular form.

Implementing the Logic: Code Snippet

Now, let’s translate this logic into a C program! Here’s a code snippet for printing a pyramid star pattern:


#include <stdio.h>

int main() {
  int rows, i, j, spaces;

  // Get number of rows from user
  printf("Enter number of rows: ");
  scanf("%d", &rows);

  // Loop for each row
  for (i = 1; i <= rows; i++) {
    // Calculate number of spaces
    spaces = rows - i;

    // Print spaces
    for (j = 0; j < spaces; j++) {
      printf(" ");

    // Print stars
    for (j = 0; j < i; j++) {


  return 0;


  1. We declare variables for the number of rows (rows), loop iterators (i and j), and number of spaces (spaces).
  2. We prompt the user for the desired number of rows and store it in rows.
  3. The outer loop iterates from 1 to rows to control the number of rows printed.
  4. Inside the outer loop, we calculate the number of spaces required for each row (spaces = rows - i). This ensures more spaces for upper rows and fewer for lower rows, creating the pyramid shape.
  5. Another loop iterates spaces times and prints a space character each time.
  6. Finally, a loop iterates i times (equal to the current row number) and prints a star character each time.
  7. After printing all stars for the current row, we move to the next line with a newline character (\n).

Variations and Enhancements

The basic pyramid star pattern can be further customized to create even more visually appealing designs. Here are some ideas:

  • Inverted Pyramid: Change the loop direction in the outer loop to start from the highest number of rows and decrease towards 1.
  • Hollow Pyramid: Instead of printing stars in the inner loop, print spaces except for the first and last iterations, creating a hollow pyramid shape.
  • Number Pyramid: Replace the stars with increasing numbers in each row, starting with 1 at the top.
  • Colored Pyramid: Utilize ANSI escape sequences to print stars in different colors for each row.

These variations require minor modifications to the basic code structure, allowing you to experiment and create unique and eye-catching pyramid patterns.


Creating a C program to print a pyramid star pattern is a fun and educational exercise that demonstrates the power of loops, conditional statements, and basic program logic. By understanding the underlying logic and exploring different variations, you can enhance your coding skills and unleash your creativity in generating captivating visual patterns.

Feel free to experiment with the code and explore different possibilities! The world of pyramid patterns awaits your creative touch.


Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button