How to write a C Program to Print Right Angled Triangle Star Pattern with an example?. And also show you how to print the Right Angled Triangle Pattern with different symbols.
C Program to Print Right Angled Triangle Star Pattern
This C program allows the user to enter the maximum number of rows you want to print as a Right Angled Triangle star pattern. We are going to print the Right Angled Triangle of * symbols until it reaches the user-specified rows.
#include <stdio.h> int main() { int Rows, i, j; printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); printf("\nPrinting Right Angled Triangle \n \n"); for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= i; j++ ) { printf("* "); } printf("\n"); } return 0; }
Let us see the Nested for loop
for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= i; j++ ) { printf("* "); } printf("\n"); }
Outer Loop – First Iteration: From the above screenshot, you can see the value of i is 1 and Rows is 7, so the C Programming condition (i <= 7) is True. So, it will enter into the second for loop.
Inner Loop – First Iteration
The j value is 1 and the condition (j <= 1) is True. So, it will start executing the statements inside the loop. The following statement will print * as Output.
printf("* ");
The following statement will increment the Value of the Number by 1 using the Increment Operator
j++
It will happen until the condition inside the inner for loop fails. Next, the iteration will start from the beginning until both the Inner Loop and Outer loop conditions fail.
C Program to display Right Angled Triangle Star Pattern
This program allows the user to enter the Symbol and the number of rows you want to print.
#include <stdio.h> int main() { int Rows, i, j; char Ch; printf("Please Enter any Symbol\n"); scanf("%c", &Ch); printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); printf("\nPrinting Right Angled Triangle \n \n"); for ( i = 1 ; i <= Rows; i++ ) { for ( j = 1 ; j <= i; j++ ) { printf("%c", Ch); } printf("\n"); } return 0; }
Please Enter any Symbol
$
Please Enter the Number of Rows: 12
Printing Right Angled Triangle
$
$$
$$$
$$$$
$$$$$
$$$$$$
$$$$$$$
$$$$$$$$
$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$$$$
C Program to display Right Angled Triangle Star Pattern using While Loop
In this C program, we just replaced the For Loop with the While Loop. I suggest you refer While Loop article to understand the logic.
#include <stdio.h> int main() { int Rows, i = 1, j; char Ch; printf("Please Enter any Symbol\n"); scanf("%c", &Ch); printf("Please Enter the Number of Rows: "); scanf("%d", &Rows); printf("\nPrinting Right Angled Triangle \n \n"); while ( i <= Rows) { for ( j = 1 ; j <= i; j++ ) { printf("%c", Ch); } printf("\n"); i++; } return 0; }
Please Enter any Symbol
*
Please Enter the Number of Rows: 12
Printing Right Angled Triangle
*
**
***
****
*****
******
*******
********
*********
**********
***********
************