The pleasure of solving!

It's been a quite long since I blogged something :) May be a writer's block* (*acronym for pure laziness,here);) Well, Last Saturday, I went to dxmk9 program conducted by NSS collage of engineering at Kottamaidhanam, Palakkad. I had registered my name for 'Geek's Paradise' competition which is basically a C programming contest. The first task was to answer some objective questions, mainly to predict the output, was not much tough. The second task was to print a number pattern like this:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1

whooo! the problem was not easy for a newbie like me ('m still in school,sir ;) ), I simply wrote some printf() statements with some ++i,etc. and escaped! Also, I had no much time to think, I had promised to return home before 1 PM.

When I reached home, a sudden thought flashed across my mind "Why shouldn't I give a try ?". I booted my ubuntu system ( 'm eagerly waiting to get Fedora in hands), took newly installed Netbeans IDE and started trying. After some time -  LOL :) !! It worked!! The program asked for an integer input and printed the pattern exactly the way it's meant to be! I know, this is a small problem and has nothing of great speciality or something, but don't know why - The output gave me some moments of rejoice! A small pleasure of coding :) Ha ha, I would like to share that code with you :)

/*
 *      121.c
 *
 *      Copyright 2009 Ershad K <[email protected]>
 *
 *      Licensed under GPL Version 3
 */

#include <stdio.h>

main() {

    int n;
    int i = 1;
    int j = 1;
    printf("Enter a number: ");
    scanf("%d",&n);

    // Increment....
    for (;i <= n; i++ )
    {
        for(j = 1; j <= i; j++)
        {
            printf(" %d",j);
        }
        --j;

        for (--j; j >=1; j--)
        {
            printf(" %d",j);
        }
        printf("n");
    }

    // Decrement....

     for (i = n-1;i >= 1; i-- )
    {
        for(j = 1; j <= i; j++)
        {
            printf(" %d",j);
        }
        --j;

        for (--j; j >=1; j--)
        {
            printf(" %d",j);
        }
        printf("n");
    }

}

Patches are always welcome :) All the best, nice day!

Leave a Comment