How to print Tricky Number Pattern With C programming Laguage

in #howto7 years ago

#include <stdio.h>

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

printf("Enter N: ");
scanf("%d", &N);

//First part of the pattern
for(i=1; i<=N; i++)
{
    //Prints trailing spaces
    for(j=1; j<i; j++)
    {
        printf(" ");
    }

    printf("%d", i);

    //Prints central spacces
    for(j=1; j<=((N - i) * 2 - 1); j++)
    {
        printf(" ");
    }

    //Don't print for last row
    if(i != N)
        printf("%d", i);

    //Moves on to the next row
    printf("\n");
}

//Second part of the pattern
for(i=N-1; i>=1; i--)
{
    //Prints trailing spaces
    for(j=1; j<i; j++)
    {
        printf(" ");
    }

    printf("%d", i);

    //Prints central spaces
    for(j=1; j<=((N - i ) * 2 - 1); j++)
    {
        printf(" ");
    }

    printf("%d", i);

    //Moves on to the next line
    printf("\n");
}

return 0;

}

Sort:  

Very nice post

This post received a 3.4% upvote from @randowhale thanks to @riyasaat! For more information, click here!