Print Diagonal Numbers Pattern in C

In this article, we will learn how to print a diagonal pattern with numbers. We will print the same number in the upper dialog and mix numbers in the lower diagonal of the square box.

Desired Output for Size 5:
1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5

C Program to Print Diagonal Numbers Pattern

#include<stdio.h>
int main()
{
  int num;

  printf("Please enter the size of Square Box: ");
  scanf("%d",&num);

  for(int row=1; row<=num; row++)
  {
    
    for(int col=1; col<=num; col++)
    {
      if(col <= num-row)
	 printf("%4d",1);
      else 
	 printf("%4d",row);
    } 

    printf("\n");
  } 

  return 0;
}

Output

Please enter the size of Square Box: 5
   1   1   1   1   1
   1   1   1   2   2
   1   1   3   3   3
   1   4   4   4   4
   5   5   5   5   5