In this tutorial, you will learn how to write a C program to print the right triangle star pattern.
This is how a right triangle pattern looks like:
Number of rows = 6 Output: * * * * * * * * * * * * * * * * * * * * *
Program to Print Right Triangle Star Pattern
#include <stdio.h>
int main() {
int row, column, numberOfRows=8;
for(row=0; row<numberOfRows; row++)
{
for(column=0; column<=row; column++)
{
printf("* ");
}
//This is just to print the stars in new line after each row
printf("\n");
}
return 0;
}
Output:
Explanation of the program:
- In this program, we are running a nested for loop. The outer for loop runs from 0 till
numberOfRows
, this basically 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 right triangle star pattern. - The inner loop is responsible for printing the stars in the each row. It prints one star in the first row, two stars in the second row and so on.
- After the inner loop ends 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.