Monday, October 18, 2010

Print 20 Dashes - One puzzle a day - Puzzle Buddies

Puzzle:

In the following C program, replace(not add or delete) only one character so that it would print 20 dashes.

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


printf("-");
}

Find three different solutions to this puzzle.

Solution:

1.

int i, n = 20;

for (i = 0 ; i < n ; n--) {
printf("-");
}

2.

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

3.

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

Winner: None

No comments:

Post a Comment