C program to print right triangle star pattern

In this article, I will show you, How to write a C program to print right triangle star pattern. Here, one thing is important to know that the rows of the right triangle.

Logic to write C program to print right triangle star pattern:

It is very easy to print right triangle star pattern in C, below I have mentioned a few steps to print the right triangle pattern in C:

  • Enter the row value for the right triangle.
  • There should be two-loop inner and outer.
  • The outer loop iterate row times and a print newline after completing the inner loop.
  • If you will look at the design carefully, then find that star is increasing in each row according to the row. So inner loop print the star x times.

See the, C program to print right triangle star pattern:

 

#include <stdio.h>

int main()
{
    int x = 0,y = 0;
    unsigned int rows = 0;

    printf("Enter the number of rows = ");
    scanf("%u",&rows);

    for(x=1; x<=rows; ++x)
    {
        // Print spaces
        for(y=1; y <= x; ++y)
        {
            printf("*");
        }
        // Print new line
        printf("\n");
    }
    return 0;
}

Output:

print right triangle star pattern

 

Code Analysis:

It asks the user to enter the side of the rhombus.

printf("Enter the number of rows = ");
scanf("%u",&rows);

 

The code contains the two-loop, inner loop, and outer loop.  The inner loop prints space x times. Where x is the number of rows.

for(y=1; y <= x; ++y)
{
    printf("*");
}

 

The outer loop print the newline after each iteration of the inner loops.

 

Recommended Post: