Here is a C program to print a right angles triangle pattern of star (*) character using loops. For a right triangle star pattern of 7 rows. Program's output should be:
Printing right triangle star patterns is a fundamental programming exercise that helps beginners understand the concepts of loops, conditional statements, and basic arithmetic operations in C.
Required Knowledge
- Take the number of rows(N) of right triangle as input from user using scanf function.
- Number of stars in Kth row is always K. 1st row contains 1 star, 2nd row contains 2 stars, 3rd row contains 3 stars. In general, Kth row contains K stars.
- We will use two for loops to print right triangle star pattern.
- For a right triangle star pattern of N rows, outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
- For Kth row of right triangle pattern, inner loop will iterate K times. Each iteration of inner loop will print one star (*).
Here is the matrix representation of the triangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.
C program to print right triangle star pattern
#include<stdio.h> int main() { int i,j,rows; printf("Enter the number of rows\n"); scanf("%d", &rows); for(i = 0; i < rows; i++) { /* Prints one row of triangle */ for(j = 0; j <= i; ++j) { printf("* "); } /* move to next row */ printf("\n"); } return 0; }Output
Enter the number of rows 6 * * * * * * * * * * * * * * * * * * * * *
Tips for Writing Right Triangle Star Pattern Printing Programs in C
- Use Nested Loops : To print a right triangle star pattern, use nested loops—one for rows and one for columns. The outer loop controls the number of rows, while the inner loop controls the number of stars in each row.
- Use Comments : Add comments to your code to explain the logic behind each step. This makes your code easier to understand for others (and yourself) and can be helpful when revisiting the code in the future.
- Use Variables for Size : To make the program more customizable, store the height of the right triangle in variables. You can easily change the triangle's size this way without having to change the code.
- Test Gradually : To test your program, start with a small right triangle and make it bigger over time. This lets you find and fix mistakes quickly and makes sure that your program works right for triangles of any size.
Beginners can build a strong foundation for their programming journey by studying the right triangle star pattern printing program in C. This will help them understand looping and conditional statements better, solve problems better, and make the program work better. With time and practice, anyone can learn how to print patterns and come up with a huge range of creative designs.
Related Topics