If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C, an inverted triangle pattern can be created using loops and the printf()
function. It is similar to the right-angled triangle pattern, but the number of asterisks in each row decreases. Here is an example of how to create an inverted right-angled triangle pattern of asterisks:
Copy code
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
This code uses nested loops to create an inverted right-angled triangle pattern of 5 lines. The outer loop controls the number of rows, starting at 5 and decreasing by 1 in each iteration. The inner loop controls the number of asterisks in each row, which is equal to the current value of the outer loop variable.
You can change the number of rows of the triangle by changing the initial value and the condition of the outer loop variable. For example, to create a triangle of 8 lines you can change the initial value of the outer loop variable to 8, like this:
Copy code
for (int i = 8; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
As you can see, the code for the inverted triangle is similar to the right-angled triangle, but the initial value and the condition of the outer loop is changed to have the inverted triangle pattern.
You can also use different characters or symbols to create different patterns.
Comments: 0