Program to print down up number pattern

Program to print down up number pattern

This program can print the down up number pattern using c.

Logic:

  • Get the row size from the user.
  • Below steps are executed for (2*n)-1 times.
  • Determine starting position to store the count value.
  • Then decrement row value and increment column value.
  • At last print the array to display pattern.

Program:

#include<stdio.h>
void main()
{
    /* a->stores the pattern value
       n->number of rows
       k->determines the row
       l->determines the column
       cnt->value to store
    */
    int a[10][10],n,i,j,k,cnt=1,l,m;
    printf("Enter the number:");
    scanf("%d",&n);
    for(i=0;i<(2*n)-1;i++) //loops executes for (2*n)-1 times { k=i,m=i; l=0; if(i>=n)                      //applies only for lower half
        {
            l=(i+1)%n;
            k=n-1;
            m=n-1;
        }
        for(j=l;j<=m;j++)
        {
            a[k][j]=cnt++;            //store the count into array
            k--;
        }
    }
    for(i=0;i<n;i++)                 //prints the pattern
    {
        for(j=0;j<n;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}

You might also like…

Program to print the x-cross pattern

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.