COME ON CODE ON

A blog about programming and more programming.

Just Another Question

with 8 comments

Here’s an interesting Question :
Look at the following code. Its to print ‘*’ 20 times.
#include
using namespace std; 
int main()
{
    int i,n=20;
    for (i=0;iAnswer 1: Change i<n to -i<n
Answer 2: Change i- – to n- –
Answer 3: Change i<n to i+n

Credit for this question goes to my roommate who gave me this(though he too got it from somewhere else).

-fR0D

Advertisements

Written by fR0DDY

March 5, 2009 at 6:09 AM

Posted in Uncategorized

Tagged with ,

8 Responses

Subscribe to comments with RSS.

  1. this was a good one.

    gaurav

    March 18, 2009 at 6:18 PM

  2. first one:
    #include
    using namespace std;
    int main()
    {
    int i,n=20;
    for (i=0;-i<n;i–)
    cout<<"*";
    return 0;
    }
    second one:
    #include
    using namespace std;
    int main()
    {
    int i,n=20;
    for (i=0;i<n;n–)
    cout<<"*";
    return 0;
    }

    not able to think abt 3rd one.. tried a lot..:(
    plz mail me the 3rd method
    agrawal.tarun23@gmail.com

    Tarun Kumar

    September 29, 2009 at 12:51 AM

    • The answer is given in the post itself. Just try to highlight.

      fR0D

      September 29, 2009 at 7:06 AM

  3. Nice One fR0D
    looking forward to more of these

    Mayank

    October 2, 2009 at 3:20 PM

  4. for (i=0; i + n; i–)

    note the LT was replace by ‘+’

    st0le

    November 7, 2009 at 5:43 PM

  5. maybe this will work too

    for (i=0;~i<n;i–)

    st0le

    November 7, 2009 at 5:51 PM

  6. Very similar as website from Berkeley.
    Look for “FOR LOOP TWEAKS”, if site not work try as cache view in google.

    McQWZYX

    December 7, 2009 at 1:45 AM

  7. hmm..i found a fourth way! 🙂

    Aadith

    January 9, 2013 at 3:26 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: