Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

question was asked in aptitude test

  Asked By: Harley    Date: Jun 19    Category: Java    Views: 1195
  

This question was asked in aptitude test.


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. What
are
they ?

Duration: 5 min.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Jo Fowler     Answered On: Jun 19

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

 
Answer #2    Answered By: Blaze Fischer     Answered On: Jun 19

adding 4
for(i = 40; i < n; i--)

adding -
int i, n = -20;

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

 
Answer #3    Answered By: Pam Harrison     Answered On: Jun 19

First
int i, n = 20;
for (i = 0; -i < n; i--)
printf("-");

Second
int i, n = 20;
for(i = 0; i < n; n--)
printf("-");

third
adding 4
for(i = 40; i < n; i--)

 
Answer #4    Answered By: Shannon Hughes     Answered On: Jun 19

third should be i + n instead of i < n.

 
Didn't find what you were looking for? Find more on question was asked in aptitude test Or get search suggestion and latest updates.




Tagged: