Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Can you solve this one

samrat_nareshsamrat_naresh Posts: 2Member
Hi,
I'm new to C. Can you solve the following program. Print the following,

0
1 2
3 4 5 and
6 7 8 9

0
2 1
5 4 3
9 8 7 6

Comments

  • anthrax11anthrax11 Posts: 511Member
    That's easy:
    [code]
    #include

    int main()
    {
    puts ("0
    "
    "1 2
    "
    "3 4 5 and
    "
    "6 7 8 9
    "
    "
    "
    "0
    "
    "2 1
    "
    "5 4 3
    "
    "9 8 7 6"
    );

    return 0;
    }
    [/code]
  • samrat_nareshsamrat_naresh Posts: 2Member
    I want solution using for loop....
  • anthrax11anthrax11 Posts: 511Member
    You won't learn anything until you start to mess with these things yourself. All it takes is a bit of effort to do your homework. It took me 5 minutes to do this, you can probably write better code than this too:
    [code]
    #include

    int main()
    {
    int n=0, i,j;

    for (i=1; i<5; i++)
    {
    for (j=i; j>0; j--)
    {
    printf("%u ", n);
    n++;
    }
    if (n == 6)
    puts("and");
    else
    putchar('
    ');
    }

    n=0;
    for (i=0; i<5; i++)
    {
    n += i;
    for (j=1; j<=i; j++)
    {
    printf("%u ", n-j);
    }
    putchar('
    ');
    }

    return 0;
    }
    [/code]
Sign In or Register to comment.