C program to print Equilateral triangle (Pyramid) star pattern

Write a C program to print the equilateral triangle or Pyramid star(*) pattern series of n rows using for loop. How to print Pyramid star pattern series using for loop in C programming. Logic to print pyramid star pattern series in C program.

Example

Input

Input rows: 5

Output

Required knowledge

Basic C programming, For loop

Logic to print pyramid star pattern

There can be many ways of thinking of the problem here I am discussing the easiest one. We need to print two things one is the trialing spaces and other is the equilateral triangle. As like other star pattern problems here also spaces are arranged in a special way i.e. each row contains n - row_number spaces (where n is the total number of rows).

When you look to the total number of stars(*) in the equilateral triangle pattern you will notice that each row contains 2*rownumber - 1 stars.

Program to print pyramid star pattern series

/**
 * C program to print equilateral triangle or pyramid star pattern
 */

#include <stdio.h>

int main()
{
    int i, j, n;

    // Input number of rows to print
    printf("Enter number of rows : ");
    scanf("%d", &n);

    for(i=1; i<=n; i++)
    { 
        // Print trailing spaces
        for(j=i; j<n; j++)
        {
            printf(" ");
        }

        // Print the pyramid pattern
        for(j=1; j<=(2*i-1); j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
    *
   ***
  *****
 *******
*********

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>