## Basic

Moderators: None (Apply to moderate this forum)
Number of posts: 4766

This Forum Only

Can you solve this one Posted by samrat_naresh on 20 Aug 2009 at 1:49 AM
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

Re: Can you solve this one Posted by anthrax11 on 20 Aug 2009 at 2:22 AM
That's easy:
```#include <stdio.h>

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

return 0;
}
```
Re: Can you solve this one Posted by samrat_naresh on 20 Aug 2009 at 7:12 AM
I want solution using for loop....
Re: Can you solve this one Posted by anthrax11 on 21 Aug 2009 at 12:13 PM
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:
```#include <stdio.h>

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');
}

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

return 0;
}
```

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic