C Program to Print Pyramid Star Pattern

Here is a C program to print an equilateral triangle star pattern of n rows using loops.

For an equilateral triangle pyramid star pattern of 5 rows. Program's output should be:
C program pyramid star pattern

The pyramid star pattern printing program in C is a classic and foundational exercise that helps beginners understand key programming concepts such as loops, conditional statements, and variable manipulation. In this article, we will delve into the advantages of this program, its significance for beginners, tips for effective implementation, and a conclusion summarizing its educational value.

Required Knowledge

Algorithm to print pyramid star pattern using loop
In this program, we are printing a pyramid star pattern in which ith row contains (2*i + 1) space separated stars. The index of rows and columns starts from 0.
  • We first take the number of rows(R) in the pattern as input from user using scanf function.
  • One iteration of outer for loop will print a row of pyramid(from i = 0 to R - 1).
  • For jth row of pyramid, the inner for loop first prints (R - i - 1) spaces for every line and then nested while loop prints (2*j + 1) stars.

Here is the matrix representation of the pyramid star pattern. The row numbers are represented by i whereas column numbers are represented by j.

C program pyramid star pattern

C program to print pyramid star pattern

#include<stdio.h>

int main() {
    int i, j, rows, star = 0;
    printf("Enter the number of rows\n");
    scanf("%d", &rows);

    /* printing one row in every iteration */
    for (i = 0; i < rows; i++) {
        /* Printing spaces */
        for (j = 0; j <= (rows - i - 1); j++) {
            printf(" ");
        }
        /* Printing stars */
        while (star != (2 * i + 1)) {
            printf("*");
            star++;;
        }
        star = 0;
        /* move to next row */
        printf("\n");
    }
    return 0;
}
Output
Enter the number of rows
6
     *
    ***
   *****
  *******
 *********
***********

C program to print pyramid star pattern using single for loop

#include<stdio.h> 

int main(){
    char *str="*****************";
    int i,j, rows;
    
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
      
    for(i=0;i<rows;i++){
       printf("%*.*s\n",rows+i, 2*i+1, str);
    }
    return 0;
}
Output
Enter the number of rows
6
     *
    ***
   *****
  *******
 *********
***********

Advantages of the Pyramid Star Pattern Printing Program in C

Now, let's discuss the advantages of the Pyramid Star Pattern Printing Program in C. Understanding these advantages will help you appreciate the importance of practicing this program:
  • Improvement of Debugging Skills : Debugging this program helps you understand common programming errors and how to fix them. This improves your ability to read and understand error messages, which is essential for debugging more complex programs in the future.

  • Enhancement of Problem-solving Skills : This program challenges you to break down a complex pattern into smaller, manageable parts. By figuring out how to print a pyramid of stars, you'll develop your problem-solving skills, which are crucial in programming and beyond.

  • Encouragement of Creativity : While the pyramid pattern may seem simple, there are many ways to customize it and create unique designs. This encourages creativity and allows you to explore different ways of solving the same problem.

  • Reinforcement of Looping Concepts : The pyramid pattern requires the use of nested loops, where one loop controls the number of rows and another controls the number of stars in each row. This reinforces your understanding of loops, a fundamental concept in programming.

Conclusion

In conclusion, the Pyramid Star Pattern Printing Program in C is a good way for beginners to improve their ability to think of solutions to problems, solidify their understanding of loops, get better at finding bugs, and get creative with their programming. Beginners should use this program to get a feel for basic programming ideas and boost their confidence in their programming skills. Beginners can write this program well and build a strong base for their programming journey by following the tips given.


Related Topics
C program reversed pyramid star pattern
C program hollow pyramid star pattern
C program hollow diamond star pattern
C program right triangle star pattern
C program hollow square star pattern
C program heart shape star pattern
C program natural number triangle pattern
C program palindrome triangle pattern
C program same row element triangle pattern
List of all C pattern programs