# Thread: find out logic for the program

1. ## find out logic for the program

required output:
Code:
```  1
2 2
3 3 3```
can any one please tell me the logic for the required output ?????

2. This is the first and probably last time I'm going to do someone's homework:
Code:
```#include <stdio.h>
void /* To fail even harder */ main()
{
printf("  1\n 2 2\n3 3 3\n");
}```

3. Originally Posted by cooldude
can any one please tell me the logic for the required output ?????
Yes, you must print three lines like this:

Code:
```  1
2 2
3 3 3```
@EVOEx: the return type of main() should be int, not void

4. You must know, since you're able to do it, right? How'd you make that triangle of digits for your post?

Maybe you took the first number (1) and divided the width of the base of the triangle (which is not even drawn yet), by two.

Idea's in pseudo-code.

Code:
```begin outer loop: (maybe for(i = 0; i < 4; i++) {

begin loop for each row:

spacesBetweenDigits = SizeofBaseOfTriangle / (numberToPrint+1)

print spacesBetweenDigits number of spaces.

then print numberToPrint and increment numberYouPrinted by one.

loop until numberYouPrinted == numberToPrint

print newline

} loop until all the rows have been printed```
Once you get down the steps you can use, in simple terms, then it's easy to start changing it into code, although you'll undoubtedly change it as you go. Thing is, you'll have a starting point, for your program.

5. k thanks a lot guys.........

6. Originally Posted by MK27
@EVOEx: the return type of main() should be int, not void
That was a joke... right?

7. Actually, it is a good practice to make the return type of main() int:

Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])

8. Originally Posted by MTK
Yes, but that's why I put the comment "To fail even harder" in there. Trust me, if I do someone's homework, I make sure they'll fail as badly as they can.

9. Okay, I really did it for you this time. 109 chars. Anybody think they can do better? I dare you all ;-).

Code:
`c;main(a,b){a-1||(b=getchar()-48);printf("%*d",b--,c=a);while(--c)printf(" %d",a);puts("\n");b&&main(a+1,b);}`
(note: input through stdin. DONT try to pass it as a parameter ;-)

Edit: 103 (I'm bored)

Code:
`c;main(a,b){b=a-1?b:getchar()-48;for(printf("%*d",b--,c=a);c--;printf(c?" %d":"\n",a));b&&main(a+1,b);}`
And last for 101
Code:
`c;main(a,b){for(printf("%*d",b=a-1?b:getchar()-48,c=a);c--;printf(c?" %d":"\n",a));--b&&main(a+1,b);}`
I just HAD to break the 100-score. So for 99:
Code:
`b,c;main(a){for(printf("%*d",b=a-1?b:getchar()-48,c=a);c--;printf(c?" %d":"\n",a));--b&&main(a+1);}`
Come on, nobody else daring a game of programming golf?

10. Originally Posted by EVOEx
Yes, but that's why I put the comment "To fail even harder" in there. Trust me, if I do someone's homework, I make sure they'll fail as badly as they can.
Oh, I didn't get that at first.