Programming puzzles

sid_gupta

Broken In
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).
 

root.king

geek in action
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

Destroy Erase Improve
Staff member
Admin
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.
 
OP
S

sid_gupta

Broken In
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
 

Nue

...
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

I am the night...I 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

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.
 

Nue

...
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.
 
Top Bottom