C program to print triangle pattern having same number in a row

  • Write a program in C to print right triangle having same number in a row.
For a right triangle of 6 rows, program's output should be:
C program same row element triangle pattern

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 same row element triangle pattern

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
C program natural number triangle pattern
C program palindrome triangle pattern
C program rhombus star pattern
C program binary triangle pattern
C program hollow square star pattern
C program right triangle star pattern
C program mirrored right triangle star pattern
C program inverted right triangle pattern
C program reversed pyramid star pattern
List of all C pattern programs