CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2005
    Location
    On the Dark Side of Moon
    Posts
    86

    Thumbs up 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

    Problem Is An Opportunity To Do Your Best.

    All Code Guru Member's
    Poll : -Please Vote For Code Guru Forum Programming competition


  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    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 ?

  3. #3
    Join Date
    Jun 2008
    Posts
    0

    Question Re: Small Technical puzzles……?

    Does ANYBODY has the answer to this QUESTION, Please?? It's making me eager.............

  4. #4
    Join Date
    Oct 2011
    Posts
    0

    Smile Re: Small Technical puzzles……?

    void main()
    {int x=1;
    start:if(x=1)
    {printf("Hello");
    x++;
    goto start;
    }
    else
    printf("World");
    }

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Small Technical puzzles……?

    Quote Originally Posted by pratikamlani View Post
    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.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Small Technical puzzles……?

    Equals != Equals Equals

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured