If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C, a triangle pattern can be created using loops and the printf()
function. There are different types of triangle patterns that can be created, such as a right triangle, an isosceles triangle, or a equilateral triangle. Here is an example of how to create a right-angled triangle pattern of asterisks:
Copy code
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
This code uses nested loops to create a right-angled triangle pattern of 5 lines. The outer loop controls the number of rows and the inner loop controls the number of asterisks in each row. The number of asterisks in each row is equal to the row number, so the first row has 1 asterisk, the second row has 2 asterisks, and so on.
You can change the number of rows of the triangle by changing the value of the outer loop variable and condition. For example, to create a triangle of 8 lines you can change the condition of the outer loop to 8, like this:
Copy code
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
Comments: 0