Just another tech blog.

A blog abt GNU/Linux, programming, hacking, and my life.

Some good looking codes and interesting problems

Posted by ajay on July 21, 2006

I recently came across some awesome piece of codes. I dont understand any of them, but I find it worth sharing here.

1. A c program to solve The Tower of Hanoi problem without using functions,  stack etc. and does the task in 6 lines. Moreover, the code looks like a tower as well :).

“disk %i from %i to %i.\n”/**/

2. The queen of problems – N queens problem. If you are a computer science student, you must have heard about n queens problem and probably solved that as well.  This C program solves the problem very quickly, and for the rest – just have a look at the code.

int v,i,j,k,l,s,a[99];main(){for(scanf(“%d”,&s);*a-s;v=a[j*=v]-a[i],k=i<
s,j+=(v=j<s&&(!k&&!!printf(2+”\n\n%c”-(!l<<!j),” #Q”[l^v?(l^j)&1:2])&&++

3. Prints first 15000 digits of PI.


4.  An interesting problem –

what is the “condition” such that, this snippet of code prints HelloWorld!!

if condition




my friend abhilash gave a nice solution for that ..

if ( printf(“Hello”)<0){




5. Another interesting problem –

Write a small C program, which while compiling takes another program
from input terminal, and on running gives the result for the second
program. (NOTE: The key is, think UNIX).
Suppose, the program is answer.c
Then, while compiling
$ cc -o answer answer.c
int main()
printf(“Hello World\n”);
$ ./answer

Hello World
if anybody can solve this problem please let me know by commenting over the post with the solution.

Happy coding.


23 Responses to “Some good looking codes and interesting problems”

  1. Atul said

    gud work dude………
    but I didn’t get what does if(printf(“Hello”) >0 ) );
    plz mention that also..

  2. ajay said

    @atul plzz mention that also

    coz printf(“Hello”) returns 1 so this condition will be true and in that case the output would be


    which we don’t want .. so we need to somehow make that if condition fail 🙂 .. I hope its clear now

    • Anisha said

      Per pedro navajaSono contento di risentirti!Grazie delle precisazioni. Volevo solo sottolineare come sia discutibile e semplicistico il luogo comune secondo cui le “democrazie liberali mercantili” sarebbero naturalmente pacifiche, perché vogliono commerciare, mentre gli altri regimi – confusamente accomunati sotto l’etichetta “dittature” – sarebbero portati a interminabili guerre.Da cui, secondo i neocon la necessità di fare la… guerra alle “dittature” per pacificarle a forza, secondo i neolib la necessità di sovvertirle con &#ori0;2iv8luz2oni”.

  3. Turbo said

    Keep coming with these kinds of posts.

  4. sharatechno said

    gud work dude

  5. bluecap said

    its gr* work keep it up

  6. bluecap said

    its gr8
    work keep it up

  7. navin said

    For the last question , modify the
    $ gcc -o ans -xc –
    int main()
    printf(“hello world\n”);

    well you can alias cc or write a script for that.Hope this solves your problem

  8. nik said

    /* obfuscated c contest 1988 prize winner Spinellis.c */

  9. harsha said

    hi there,
    nice works buddy. i loved the tower of hanoi problem for which u hve given a nice code.
    hey, can u help me by sending c-codes for the linux commands awk,grep,pipe.


  10. ajay said

    I don’t have c codes for such utilities ( why write them if you have the commands ? ). :d

  11. Siddharth said

    U can find more of such interesting codes by googling for IOCCC ,:).

  12. aravindakella said

    Dude … ur blog rocks,I hav seen u in a lot of OPCs.
    Keep up the good work.

    • Storm said

      Hm, láttam a videót is, gratula — szerintem nagyon meggyÅ‘zÅ‘ ez a koÃnÃbm¡cii³. Nem semmi pénzbe kerül, de attól eltekintve abszolút tetszik. Az iPhone nagyon jó vas, és a TomTom szépen integrálta a szoftverét az iPhone felülete alá. Gyors, elegáns, szép. Köszi a beszámolót!

    • Quer dizer que Jundiaí tem uns favelões tensos, né? E tem uns putos de lá que ainda torcem o nariz quando você diz que mora Francisco Morato (aka Chico City), mereço! hahaha.Curti o episódio.

  13. checkout
    for amazing obfuscated codes.. they look so beautiful.. 😛

    #define X
    #define XX
    #define XXX
    #define XXXX
    #define XXXXX
    #define XXXXXX
    #define XXXXXXX
    #define orfa for
    #define XXXXXXXXX
    #define archa char
    #define ainma main
    #define etcharga getchar
    #define utcharpa putchar

    X X
    X X X X
    X X X X
    X X X X
    X X X X
    X X X X
    X X X X
    X X X X X X
    X XX X X XX X
    X XXXX X XX ainma(){ archa XX X XXXX X
    X XXXX X oink[9],*igpa, X XXXX X
    X XXXXXX atinla=etcharga(),iocccwa XXXXXX X
    X XXXX ,apca=’A’,owla=’a’,umna=26 XXXX X
    X XXX ; orfa(; (atinla+1)&&(!((( XXX X
    X XX atinla-apca)*(apca+umna-atinla) XX X
    X X >=0)+((atinla-owla)*(owla+umna- X X
    X atinla)>=0))); utcharpa(atinla), X
    X X atinla=etcharga()); orfa(; atinla+1; X X
    X X ){ orfa( igpa=oink ,iocccwa=( X X
    X X (atinla- XXX apca)*( XXX apca+umna- X X
    X atinla)>=0) XXX XXX ; (((( X
    X atinla-apca XXXXX XXXXXXX XXXXX )*(apca+ X
    X umna-atinla XXXXXX )>=0) XXXXXX +((atinla- X
    X owla)*(owla+ XXXX umna- XXXX atinla)>=0)) X
    X &&”-Pig-” XX “Lat-in” XX “COb-fus” X
    X “ca-tion!!”[ X (((atinla- X apca)*(apca+ X
    X umna-atinla) X >=0)?atinla- X apca+owla: X
    X atinla)-owla X ]-‘-‘)||((igpa== X oink)&&!(*( X
    X igpa++)=’w’) X )||! X (*( X igpa X ++)=owla); * X
    X (igpa++)=(( X ( XXX XXX X atinla-apca X
    X )*(apca+ X umna XXX – XXX X atinla)>=0) X
    X ?atinla- X apca XXX + XXX owla X :atinla), X
    X atinla= X X X X etcharga()) X
    X ; orfa( X atinla=iocccwa?(( X (atinla- X
    X owla)*(owla+ X umna-atinla)>=0 X )?atinla- X
    X owla+apca: X atinla): X atinla; ((( X
    X atinla-apca)* X (apca+umna- X atinla)>=0)+( X
    X (atinla-owla)* X (owla+ X umna-atinla)>= X
    X 0)); utcharpa( XX XX atinla),atinla X
    X =etcharga()); XXXXXXX orfa(*igpa=0, X
    X igpa=oink; * igpa; utcharpa( X
    X *(igpa++))); orfa(; (atinla+1)&&(!((( X
    X atinla-apca )*(apca+ X
    X umna- XXXXX XXXXX atinla)>=0 X
    X )+(( XXXXX atinla- X
    XX owla)*( owla+umna- XX
    XX atinla)>=0))); utcharpa XX
    XX (atinla),atinla= XX
    XX etcharga()); } XX

  14. woops… couldn’t paste it properly

  15. Jigsaw said

    Hey for that problem
    “$ cc -o answer answer.c
    int main()
    printf(”Hello World\n”);
    $ ./answer

    Hello World”

    The trick lies in smart usage of include files
    answer.c contains
    #include “stdin”

    So while preprocessing it tries to read from the std input till it encounters the end of file and substitutes the #include…. with the file content. Then that’s preprocessed again and compiled.
    Done.. that’s it

  16. pavansunny said

    bhai somani i want to know how to do primality testing for a no consisting upto 1000 digits in some what efficient way ?
    please help !!!!!!!111

    @ pavan.sunny52@gmail.com

  17. Vishwas said

    Answer to your question numbered 5:

    within answer.c just write a line #include “/dev/tty”

    when compiled through cc it waits for the user to give some lines to compile and then fills the output file for those lines you key in.
    So ./answer will generate output for second program.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: