Home > Algorithms, Computer Programming, Computer Science > Try your coding skills !

Try your coding skills !

Here is a small C/C++ code snippet,

int n = 20;

for( int i = 0; i < n ; i—) // i minus minus(in case of typo)

printf(“Hello World!”) ;

First try to find the answer to this program. It will obviously be an infinite loop or memory over flow or …

The question is to make this snippet work and print the “Hello World!” 20 times correctly as if the statement i— would have been i++.

The restriction however is to change only one ASCII character in this code. This means you can change, replace, move, introduce only one character to this program and the program still remains a valid C/C++ code.

There are three possible ways I know to do this. May be there are more. If you find two then you are good, if three you are brilliant and if more than extraordinary 😉

Have happy time solving this.

  1. aristotlethegeek
    January 17, 2008 at 11:27 pm

    Solution 1:
    Change the loop exit condition to -i < n from i < n. Since -(-20) is not less than 20, the loop will end on its 20th iteration.

    Solution 2:
    Change i– to n–. So, while i remains 0, n will keep reducing till it touches 0.

    Solution 3:
    Change the loop exit condition to ~i < n from i < n. The not operator reverses the bits. So 0 becomes -1, -1 becomes 0, -2 becomes 1 and so on.

  2. January 18, 2008 at 7:07 am


    Your 3rd solution would make the program to print 21 times. because ~i starts with -1.

    However, if you make the condition “i + n” it will become 0 when i=-20. So that would be the third solution.


    Do you have any other solution than for these three?

  3. aristotlethegeek
    January 18, 2008 at 4:52 pm

    Yes. You are right. So ‘i + n’ is the third solution which is valid C/C++ (but invalid C#, which is the language I was using to tinker around). ~i was a stupid idea considering that the first solution was -i.

  4. January 19, 2008 at 1:39 pm

    Dumindu’s solution is what exactly I know, so you are right, though this wont work in C# (the i + n one) hence aristotlethegeek is also right. So you both have got the right solution :)…good shot.

  5. Fayyaz Lodhi
    February 14, 2008 at 2:20 pm

    Since we are strictly restricting to the fact of finding different ways, i would just add i – n is yet another correct one since i thought of this instead of i + n as my second solution. Nonetheless they seem like one solution presented with another operator so still they look 3 to me and i did those in less than 15 min. Any other?

  6. February 15, 2008 at 6:59 am

    Fayyaz I don’t think ‘i-n’ will work.

    Btw was it really necessary to tell the time it took to solve..?


  7. Hardik
    February 16, 2008 at 4:25 am

    if ( )


    using which condition I will above o/p?

  8. February 21, 2008 at 8:07 am

    2 very simple ways… since we can introduce characters
    1. make n= -20 (instead of 20)
    2. make int i=0 int i=40

  9. February 22, 2008 at 12:33 am

    Hmm, wouldn’t replacing i– with n– do the trick as well? eventually, n will drop to 0.

  10. February 22, 2008 at 12:34 am

    Oh, already suggested!

  11. February 26, 2008 at 6:47 am

    Ayesha!! you still can do coding :)|?, thats cool (Y)…well the answer of yours is right..

  12. March 2, 2008 at 8:11 pm

    Coding is in my blood…okay, that was cheesy. You can’t forget the khawaari of four years that easily. Trust me. 😛

  1. No trackbacks yet.

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: