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 5 rows, program's output should be:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

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.

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

#include<stdio.h>
 
int main() {
   int i, j, rows;
   int count = 1;
   
   printf("Enter the number of rows\n");
   scanf("%d", &rows);
    
   for (i = 1; i <= rows; i++) {
      for (j = 1; j <= i; j++) {
         printf("%d ", i);
      }
      printf("\n");
   }
   return(0);
}
Output
Enter the number of rows
5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

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