DIGIT-ARC read-only archive · snapshot: July 23, 2026

Programming puzzles

9 posts · originally from https://geek.digit.in/community/threads/programming-puzzles.177570/

S
sid_gupta Original post #1
Sep 7, 2013 at 2:38 AM
Post some interesting " find the output " type questions or any other sort of tricky programs here in c++ or java....

take the following code:

int i, n = 20;
for (i=0; i<n; i--)
{
cout << "x" << endl;
}

by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following:

find 3 ways to make the above code print x 20 times (by changing only one character or inserting one char).
harshilsharma63
Sep 7, 2013 at 10:24 AM
Tell the output:

float f=1.22;
if(f == 1.22)
printf("True");
else
printf("False");
root.king
Sep 7, 2013 at 6:18 PM
Post some interesting " find the output " type questions or any other sort of tricky programs here in c++ or java....

take the following code:

int i, n = 20;
for (i=0; i<n; i--)
{
cout << "x" << endl;
}

by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following:

find 3 ways to make the above code print x 20 times (by changing only one character or inserting one char).

int i, n = 20;
for (i=0; i<n; i--)
{
count << "x" << endl;
}
Desmond
Desmond #5
Sep 7, 2013 at 11:47 PM
Post some interesting " find the output " type questions or any other sort of tricky programs here in c++ or java....

take the following code:

int i, n = 20;
for (i=0; i<n; i--)
{
cout << "x" << endl;
}

by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following:

find 3 ways to make the above code print x 20 times (by changing only one character or inserting one char).

if i=0, why are you doing a i--? that will go into an infinite loop.
S
Sep 8, 2013 at 10:54 AM
well that is the the question....
you have to figure out 3 diff. ways to make this loop print 20 times by changing only one character in the code.....
hint :- you can also change whitespaces
N
Nue #7
Sep 8, 2013 at 2:29 PM
Post some interesting " find the output " type questions or any other sort of tricky programs here in c++ or java....

take the following code:

int i, n = 20;
for (i=0; i<n; i--)
{
cout << "x" << endl;
}

by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following:

find 3 ways to make the above code print x 20 times (by changing only one character or inserting one char).

Here's one way to do it.
PHP:
int i, n = 20; 
for (i=0; i<n; n--)  
{ 
   cout << "x" << endl; 
}
vickybat
Sep 8, 2013 at 8:15 PM
well that is the the question....
you have to figure out 3 diff. ways to make this loop print 20 times by changing only one character in the code.....
hint :- you can also change whitespaces

Here's one solution. You tell the other two:

Code:
 int n = 20;
        for(int i = 0; [B][SIZE=4]-i[/SIZE][/B]<n;i--)
        {
            cout << "x" << endl; 
            
        }

Just add a "-" prefix to i.
N
Nue #9
Sep 9, 2013 at 2:52 PM
Here's one way to do it.
PHP:
int i, n = 20; 
for (i=0; i<n; n--)  
{ 
   cout << "x" << endl; 
}

Okay so here's another method:
PHP:
int i, n = 20;  
for (i=0; i+n; i--)   
{  
    cout << "x" << endl;  
}
So that's 2 and + 1 vickybat's answer. I guess your puzzle's been solved.