CrazyEngineers
  • Kaustubh
    Kaustubh

    MemberJun 14, 2006

    Try The 'C' Code !

    Here comes a challenge for Computer Engineers !

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

    By only changing or adding ONLY ONE character to the above code:

    Find 3 ways to make the above code print X 20 times!

    -The Big K-
    p.s: This challenge will be open for all CEans untill someone cracks it. Do *not* expect an answer from me.
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sristi

    MemberJun 15, 2006

    1)
    int i, n = 20;
    for (i = 0; i < n; i++)
    {
    printf("X");
    }


    2)int i,n=20;
    for(i=0;i++<n😉
    {
    print("X");
    }


    3)
    int i, n = 20;
    for (i = 0; ++i < =n; )
    {
    printf("X");
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorJun 15, 2006

    That was a nice attempt [​IMG]

    But you missed this -

    By only changing or adding ONLY ONE character to the above code:
    😉

    -The Big K-
    Are you sure? This action cannot be undone.
    Cancel
  • Jerry

    MemberJun 15, 2006

    working on it

    sristi
    1)
    int i, n = 20;
    for (i = 0; i < n; i++)
    {
    printf("X");
    }
    yeh, you changed -- to ++ , that is you changed two characters. However you are allowed to change only one chracter from the code. Really interesting, I'm working on it !😉
    Are you sure? This action cannot be undone.
    Cancel
  • crook

    MemberJun 16, 2006

    I'm not a C programmer but this is more of a logic problem. Should not take more time. Let me see what can I do about it.

    crook
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorJun 16, 2006

    probably I should start giving out hints!

    Cummont fellaz, do you really need them? [​IMG]

    -The Big K-
    Are you sure? This action cannot be undone.
    Cancel
  • crook

    MemberJun 17, 2006

    got it

    i think i got it.

    in the original code, make the following change

    for (i = 0; i < n; n--)

    it should work! now trying the remaining two methods 😁
    Are you sure? This action cannot be undone.
    Cancel
  • crook

    MemberJun 24, 2006

    here comes the second way - 😁

    int i, n = 20;
    for (i = 0; -i < n; i--)
    {
    printf("X");
    }
    and the third 😁

    int i, n = 20;
    for (i = 0; i + n; i--)
    {
    printf("X");
    }
    Are you sure? This action cannot be undone.
    Cancel
  • crook

    MemberJun 24, 2006

    New Challenge !

    This is a simple C puzzle for you -

    "Write a "Hello World" program in 'C' without using a semicolon."

    Try it, its very simple.

    CrooK
    Are you sure? This action cannot be undone.
    Cancel
  • Jerry

    MemberJun 25, 2006

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

    will this work? 😒

    I don't have a compiler to try this out. I have started working on crook's problem. Its been quite sometime since I letf coding 😔
    Are you sure? This action cannot be undone.
    Cancel
  • pradypop

    MemberJun 28, 2006

    main(){if(printf("Hello world")){}}

    This might print hello world. No semi-colons used.
    Are you sure? This action cannot be undone.
    Cancel
  • crook

    MemberJun 28, 2006

    yes, thats a correct solution! 😀
    Are you sure? This action cannot be undone.
    Cancel
  • rajeshkumar.km

    MemberAug 7, 2006

    my guesss

    😉
    int i, n = -20;
    for (i = 0; i < n; i--)
    {
    printf("X");
    }

    i think the above change ( minus sign before 20 in the line 'int i,n=20')will respond to your view correctly
    Are you sure? This action cannot be undone.
    Cancel
  • crook

    MemberAug 8, 2006

    rajeshkumar.km
    😉
    int i, n = -20;
    for (i = 0; i < n; i--)
    {
    printf("X");
    }

    i think the above change ( minus sign before 20 in the line 'int i,n=20')will respond to your view correctly
    😕 I'm afraid the logic won't work.

    Croook
    Are you sure? This action cannot be undone.
    Cancel
  • devi prasad

    MemberAug 10, 2006

    ans:

    first method:

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


    second method :


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


    third method:


    int i, n = 20;
    for (i = 20; i < n; i--)
    {
    printf("X");
    }


    😁
    Are you sure? This action cannot be undone.
    Cancel
  • Mahesh

    MemberOct 7, 2006

    third method:


    int i, n = 20;
    for (i = 20; i < n; i--)
    {
    printf("X");
    }



    How can this work? This is an infinite loop.
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorOct 7, 2006

    Mahesh
    third method:


    int i, n = 20;
    for (i = 20; i < n; i--)
    {
    printf("X");
    }



    How can this work? This is an infinite loop.
     for (i = 20; i < n; i--)
    Good Catch, Mahesh 😀 . Devi Prasad might want to fix it.

    -The Big K-
    Are you sure? This action cannot be undone.
    Cancel
  • bantini

    MemberOct 10, 2006

    how about (i=0;i<n:-i--)?😕 hey dude,just a lame attempt from an amateur!!!dunno much about c anyway!
    Are you sure? This action cannot be undone.
    Cancel
  • pradypop

    MemberOct 11, 2006

    Mahesh
    third method:


    int i, n = 20;
    for (i = 20; i < n; i--)
    {
    printf("X");
    }



    How can this work? This is an infinite loop.
    I don't think there's an infinite loop in the code. Infact, statements inside loop are not executed even once.

    n=20, i=20 at initilization and condition is i<n, 20<20 is not true. Condition fails straightaway.
    Are you sure? This action cannot be undone.
    Cancel
  • Mahesh

    MemberOct 11, 2006

    Still the program is wrong.Can you give correct solution?
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberOct 18, 2006

    /* 1st Way: Change i in i-- by n*/
    int i, n = 20;
    for (i = 0; i < n; n--)
    {
    printf("X");
    }


    /* 2nd Way: Add - before i in i < n */
    int i, n = 20;
    for (i = 0; -i < n; i--)
    {
    printf("X");
    }


    /* 3rd Way: Change < in i < n by + */
    int i, n = 20;
    for (i = 0; i + n; i--)
    {
    printf("X");
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Prasad Ajinkya

    MemberOct 18, 2006

    This is decent enough, hope the 3 ways are as follows -

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


    int i, n = 20;
    for (i = 40; i < n; i--)
    {
    printf("X");
    }

    int i, n = 20;
    for (i = 0; i > n; i--)
    {
    printf("X");
    }

    I have indicated in red the change in character that I have made.
    Are you sure? This action cannot be undone.
    Cancel
  • ksrbhanu

    MemberNov 2, 2006

    Hi friends,

    I have one method, i will try other two. mean while review this method

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

    replacing the i in the decrement statement with n it works , this is one character change 😎
    Are you sure? This action cannot be undone.
    Cancel
  • ksrbhanu

    MemberNov 2, 2006

    sory guys, i have n't checked the whole chain , pradeep has already posted this metho 😔
    Are you sure? This action cannot be undone.
    Cancel
  • spirit

    MemberNov 9, 2006

    hello.....i m giving your answer n u have to tell thats right or not.....😎
    this is your code........
    int i, n = 20;
    for (i = 0; i < n; i--)
    {
    printf("X");
    }
    1. instead of i-- in the loop,change it as n--.
    2.in initialisation,n=20,change it as:n=-20.
    3.instead of i--,use:i++.

    😁
    Are you sure? This action cannot be undone.
    Cancel
  • rahul dev choud

    MemberJan 16, 2007

    hey its really interesting don't tell da ans...

    working on it ....

    w'll back soon......
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register