C Puzzles

yet another place for C puzzles

Saturday, December 16, 2006

 

loop 20 times


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

Change/add only one character and print '*' exactly 20 times.
(there are atleast 3 solutions to this problem :-)

I got TWO:
1.for (i = 0; i < n--)
2.for (i = 0; i + n; i--)

Comments:
3. for (i = 0; -i < n; i--)


Hassan Shaik
 
1 for(i=0;-i * n;i--)
2 for(i=0;i+n;i--)
3 for(i=0;i * n;n--)
Note : replace * with less than sign ... i was unable to give less than sign as comments are in html

Rahul Ugale(Calsoft,Pune)
 
wow hard in beginning,but easy when answers are found
 
int i, n = 20;
for (i = 0; i < n; i++)
printf("*");
 
right answers:
"for(i=0; i+n; i--)"
for(i=0; i< n; n--)
for(i=0; -i< n; i--)
 
Would someone mind explaining the for(i=0;i+n;i--) solution? I got the other 2 on my own, but I am still a beginner at C and do not get what is going on here. Thanks
 
Keith,

after 20th iteration, i+n == -20 + 20 == ZERO.
it terminates the loop.
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2005   August 2005   October 2005   December 2005   March 2006   June 2006   July 2006   December 2006   February 2007   June 2007   March 2010   May 2010  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]