Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing a bug in a For loop

Tags:

for-loop

This is a question I was asked today at a job interview:

Look at the following code:

int n=20;
for (int i =0; i<n; i--)
    print("*");

You are allowed to change one and only one character in order for the loop to run exactly 20 times.

I wasn't able to answer the question at all. At first I thought to set i to 40, but then realized that 40 ins't smaller than 20.

My interviewer said that are 5 different answers for this question...

Please help me find the answers.

like image 853
Tal Angel Avatar asked Nov 15 '15 19:11

Tal Angel


1 Answers

Change to

int n=20;
for (int i =0; i<n; n--)
print("*");
like image 200
Adrian359 Avatar answered Sep 24 '22 20:09

Adrian359