Monday, 13 June 2016

Number pattern 3



Firstly we will take a for loop(outer) for 4 rows.In the outer for loop we need 3 for loops(spaces,i...2*i-1,2*i-1...i)
Print spaces
For every ith row , print n-i spaces.

Print numbers i....2*i-1
For every ith row , print numbers from i to 2*i-1.


Print numbers 2*i-1....i
For every ith row , print numbers from 2*i-1 to i.
#include<stdio.h>

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

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

        j = 2*i-1;

        for(k=i;k<=j;k++)
            printf("%d",k);

        for(k=j-1;k>=i;k--)
            printf("%d",k);

        printf("\n");
    }

    return 0;
}


No comments:

Post a Comment