In this tutorial, we will write a C program to print the Left Triangle Star Pattern. This is also called mirrored Right Triangle Star Pattern.
This is how a left triangle pattern looks like:
Number of rows = 6 Output: * * * * * * * * * * * * * * * * * * * * *
Program to print left triangle star pattern
#include <stdio.h>
int main()
{
int row = 0,column = 0;
int numberOfRows = 0;
printf("Enter the number of rows: ");
scanf("%u",&numberOfRows);
for(row=1; row<=numberOfRows; ++row)
{
// This loop prints white spaces before first
// star of each row
for(column=row; column<numberOfRows; ++column)
{
printf(" ");
}
// This loop prints star in each row
for(column=1; column<=row; ++column)
{
printf("*");
}
// Move the cursor to new line for next row
printf("\n");
}
return 0;
}
Output:
Explanation of the program:
In this program, we are running nested for loops. The outer for loop runs from 0 till numberOfRows, this prints the rows of the pattern. In this example the value of numberOfRows is 8 so the outer loop runs 8 times to print 8 rows of the left triangle star pattern.
There are two inner loops in this program, the first inner loop prints the whitespaces before the first star of the each row. This means this loop prints 7 whitespaces for first row, 6 whitespaces for second row and so on.
The second inner loop prints the stars in each row. It prints one star in first row, two stars in second row and so on.
After the inner loops end there is a print statement for new line. This is to move the cursor to new line for the next row.
Practice the same program in java language here.