Here is a C program to print right triangle having same number in a row. For a right triangle of 6 rows, program's output should be:
Required Knowledge
Algorithm to print triangle pattern having same number in a row using for loop
- Take the number of rows(N) of right triangle as input from user using scanf function.
- Number of integers in Kth row is always K.
- We will use two for loops to print right triangle of natural numbers.
- Outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
- For Kth row, Inner loop will iterate K times. Each iteration of inner loop will print row number on screen.
Here is the matrix representation of the above mentioned pattern. The row numbers are represented by i whereas column numbers are represented by j.
C program to print right triangle pattern having same number in a row
#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++) {
for (j = 0; j <= i; j++) {
printf("%d ", i+1);
}
printf("\n");
}
return(0);
}
Output
Enter the number of rows 6 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6
Related Topics