Welcome, %1$s. Please login or register.

February 21, 2018, 05:07:58 PM
: 1
: Change the code - Asked in Intel  ( 4696 )
« : May 12, 2007, 12:29:26 AM From Shrinidhi»



Change the code - Asked in Intel



    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. Find any 2.

PS: This was asked in Intel written test.

 
Liked It? Share it!

              


« #1 : May 18, 2007, 11:48:01 PM From Prateek»

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



One change that I could think of is this...! ( replace i with n in i-- )
Other 2?
« #2 : May 19, 2007, 11:17:34 AM From Poonam»

I have one more.

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


the i < n terminating condition in the for loop is changed to -i < n!
« #3 : May 19, 2007, 11:24:02 AM From Ria»

Nice one... What is the third one?
« #4 : May 24, 2007, 02:25:38 PM From satyadb4u»

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

let me know if i am wrong... ::)
« #5 : May 24, 2007, 03:54:03 PM From Ria»

Satya, your solution will fail during the first comparision itself in i < n since 0 < -20 is false.
Hence it won't print '*' even once.
« #6 : May 27, 2007, 02:09:47 PM From Prathik»

thn put i<=n.............it will work...........  ::)
« #7 : May 29, 2007, 11:16:29 PM From Veena»

is it the 3rd soln..

for(i=0;i>-n;i--)
pf("*");

« #8 : May 30, 2007, 07:00:41 PM From iammilind»

This is the solution

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

« #9 : June 01, 2007, 09:29:35 PM From amar»

 int main()
    {
        int i, n = 20;
        for (i = 40; i < n; i--)
                printf("*");
     return 0;
    }
« #10 : June 02, 2007, 10:14:23 AM From Prateek»

Iammilind is right. Rest all are incorrect.
: 1
« previous next »

 

Best RatedList All>>



Latest
Random



SMF 2.0.10 | SMF © 2015, Simple Machines | Contact Webmaster | OnlineFunDb.com © 2009/10 | Legal Disclaimer