-
October 3rd, 2005, 12:56 AM
#1
Small Technical puzzles……?
Hi all,
Well this Is not any competition, we all do lot of programming work but most of the
Times some small things skips even by the good experience programmer.
And these small snippet do revel the true colors of the programming language..
Well following is one of its own kinds…..
Code:
void main()
{
if (X)
printf("Hello");
else
printf("World");
}
What must be the condition (X) in "if" so as to print "HelloWorld"?
Please do post if you have one .......
Regards,
x8086
-
October 3rd, 2005, 01:18 AM
#2
Re: Small Technical puzzles……?
My guess is:
Code:
int main()
{
if (!printf("Hello"))
printf("Hello");
else
printf("World");
}
I cannot think of a way to execute both branches of conditional expression, so if only one of them can be executed, the resst has to printed during condition test.
Hob
Last edited by Hobson; October 3rd, 2005 at 01:20 AM.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
June 6th, 2008, 06:23 AM
#3
Re: Small Technical puzzles……?
Does ANYBODY has the answer to this QUESTION, Please?? It's making me eager.............
-
October 10th, 2011, 06:49 PM
#4
Re: Small Technical puzzles……?
void main()
{int x=1;
start:if(x=1)
{printf("Hello");
x++;
goto start;
}
else
printf("World");
}
-
October 10th, 2011, 08:57 PM
#5
Re: Small Technical puzzles……?
Originally Posted by pratikamlani
void main()
{int x=1;
start:if(x=1)
{printf("Hello");
x++;
goto start;
}
else
printf("World");
}
Honestly, if you're really going to violate the rules (only replace the X) to this extent, then why not simply:
Code:
int main()
{
printf("HelloWorld");
}
Despite the fact that the OP never confirmed it (in six years), Hobson's proposed solution does solve the puzzle without violating the rules.
BTW, double for using goto...
EDIT: BTW2, despite the rule violation, yours does not solve the puzzle. Actually, it's an infinite loop. I'll leave it up to you to find out why...
Last edited by Eri523; October 10th, 2011 at 09:06 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
October 13th, 2011, 05:17 AM
#6
Re: Small Technical puzzles……?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|