Home / Programs / C program to print right triangle star pattern * ** *** **** *****
Programming Example

C program to print right triangle star pattern
*
**
***
****
*****

👁 4,492 Views
💻 Practical Program
📘 Step by Step Learning
Write a C program to print right triangle star pattern series using for loop. How to print right triangle star pattern series of n rows in C programming. Logic to print right triangle star pattern in C.

Program Code

/*
 * C program to print right triangle star pattern series
* atnyla.com
 */

#include <stdio.h>

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

    /* Input number of rows from user */
    printf("Enter value of n: ");
    scanf("%d", &n);

    for(i=1; i<=n; i++)
    {
        /* Print i number of stars */
        for(j=1; j<=i; j++)
        {
            printf("*");
        }

        /* Move to next line */
        printf("\n");
    }

    return 0;
}

Output

Enter the value of n: 5
*
**
***
****
*****

Explanation

Required knowledge

Basic C programming, If else, For loop, Nested loop

Logic to print right triangle star pattern

 

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

 

If you look at the pattern carefully you will find that stars are in increasing order of rows (i.e. 1 star in the first row, followed by 2 stars in second and so on).

Step by step descriptive logic to print right triangle star pattern.

  1. Input number of rows to print from the user. Store it in a variable say N.
  2. To iterate through rows run an outer loop from 1 to N with loop structure for(i=1; i<=N; i++).
  3. To iterate through columns run an inner loop from 1 to i with loop structure for(j=1; j<=i; j++). Inside the inner loop print star.
  4. After printing all columns of a row move to next line i.e. print new line.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.