Diagonal Number Pattern in C

In this tutorial, we will learn and write a program in C to display the diagonal number pattern. So you may go through the following topic in C.

Diagonal Number Pattern in C

#include <stdio.h>

int main()
{
  int i, j, rows;

  printf("Enter the no. of rows: ");
  scanf("%d", &rows);

  for (i = 1; i <= rows; i++)
  {
    for (j = i; j < rows; j++)
      printf(" ");

    printf(" %d\n", i);
  }

  return 0;
}

Output:

Diagonal Number Pattern in C 1

#include <stdio.h>

int main()
{
  int i, j, k = 1, rows;

  printf("Enter the no. of rows: ");
  scanf("%d", &rows);

  for (i = 1; i <= (2 *rows) - 1; i++)
  {
    for (j = 1; j <= (2 *rows) - 1; j++)
    {
      if (i == j || i + j == 2 *rows)
        printf(" %d", k);
      else
        printf("  ");
    }

    if (i < rows)
      k++;
    else
      k--;

    printf("\n");
  }

  return 0;
}

Output:

Diagonal Number Pattern in C 2