1. OOT: Interesting C quiz

Got it from http://www.genesim.co.il/cprog.htm

For C programmers, here is a C code quiz. You are invited to give it a try.

/*
Find at least 4 different ways to change only one
character in the C code below so it will print the
minus sign "-" 20 or 21 times only.
*/

#include <stdio.h>

main()
{
  int i, n ;
  for(i=0; i<n; i--) printf("-");
  return 0;
}

===========================
I can only know 2 of them,
1. change i-- to n--
2. change i<n to -i<n

Can anyone help me finding the other two? I am very curious...

Thx!

new topic     » topic index » view message » categorize

2. Re: OOT: Interesting C quiz

It says it can display 21 - signs too, what about n = 21;? :P

aku saya wrote:

>
>
>OK,
>
>1. change i-- to n--
>2. change i<n to -i<n
>3. change i<n to i+n (tricky!)
>4. change i<n to ~i<n
>
>The question says there are at least 4 ways,, who knows there are
>more...??
>  
>

new topic     » goto parent     » topic index » view message » categorize

3. Re: OOT: Interesting C quiz

It says you can only change one character...
either one of the below solutions or changing n=20 to n=21..
But if you change n=20 to n=21 the result will be infinity loop.. ^^

G> It says it can display 21 - signs too, what about n = 21;? :P

G> aku saya wrote:

>>
>>
>>OK,
>>
>>1. change i-- to n--
>>2. change i<n to -i<n
>>3. change i<n to i+n (tricky!)
>>4. change i<n to ~i<n
>>
>>The question says there are at least 4 ways,, who knows there are
>>more...??

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu