Can you solve this one - Programmers Heaven

Howdy, Stranger!

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

Categories

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.