Jump to content
xisto Community
cse-icons

C Problem Interestin puzzle

Recommended Posts

Hi chk this code....int i, n = 20; for (i = 0; i < n; i--)printf("-");You have to print '-' 20 times. by adding or removing or replacing onlya single character from the code.There are 3 possible solutions for the above question. Can u get them?

Share this post


Link to post
Share on other sites

yeah, that is one solution and probably the simplest one.But as i told u there are 3 solutions in all. Can u figure out the other two?should take a little more time than the earlier one

Share this post


Link to post
Share on other sites

Hi,Without changing anything it prints already "-" twenty times and much more. I suppose it has to print exactly twenty times. right?

Share this post


Link to post
Share on other sites

int i, n = 20;

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

printf("-");

 

 

You have to print '-' 20 times. by adding or removing or replacing only

a single character from the code.

There are 3 possible solutions for the above question. Can u get them?

<{POST_SNAPBACK}>


here goes:

1. replace i with n in the for() loop

2. make it -i < n

3. in for() loop init i as 40 (for(i=40;i < 20 ... )

 

so there :-)

Share this post


Link to post
Share on other sites

hi all,

here are the 3 solutions, in case you still want to give it a try.. dont look at the solutions below:













int i, n = 20; [br]for (i = 0; i < n; n--)[/br]printf("-");

int i, n = 20; [/br]for (i = 0; -i < n; i--)[br]printf("-");

int i, n = 20; [/br]for (i = 0; i + n; i--)[/br]printf("-");

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.