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.



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.
By: aristotlethegeek on January 17, 2008
at 11:27 pm
aristotlethegeek,
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.
Saeed,
Do you have any other solution than for these three?
By: Dumindu on January 18, 2008
at 7:07 am
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.
By: aristotlethegeek on January 18, 2008
at 4:52 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.
By: Haroon Saeed on January 19, 2008
at 1:39 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?
By: Fayyaz Lodhi on February 14, 2008
at 2:20 pm
Fayyaz I don’t think ‘i-n’ will work.
Btw was it really necessary to tell the time it took to solve..?
Haroon
By: Haroon Saeed on February 15, 2008
at 6:59 am
if ( )
printf(“hi..”);
else
printf(“hello”);
O/P=hi..hello
using which condition I will above o/p?
By: Hardik on February 16, 2008
at 4:25 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
By: Amit Wadhwa on February 21, 2008
at 8:07 am
Hmm, wouldn’t replacing i– with n– do the trick as well? eventually, n will drop to 0.
By: ayesha on February 22, 2008
at 12:33 am
Oh, already suggested!
By: ayesha on February 22, 2008
at 12:34 am
Ayesha!! you still can do coding
|?, thats cool (Y)…well the answer of yours is right..
By: Haroon Saeed on February 26, 2008
at 6:47 am
Coding is in my blood…okay, that was cheesy. You can’t forget the khawaari of four years that easily. Trust me.
By: ayesha on March 2, 2008
at 8:11 pm