Find the error in the following code.

Code:

int i = 1;
while(i <= 5)
{
printf(“%d “, i);
i–;
}

Answer: The code will cause an infinite loop because the condition i <= 5 is always true, and i-- causes i to decrease, not increase. Since i starts at 1 and keeps decreasing, it will never reach a point where it exceeds 5, and the loop will never stop.

To fix this, i should be incremented instead of decremented. The corrected code should be:

int i = 1;
while(i <= 5)
{
printf(“%d “, i);
i++; // Increment i instead of decrementing it
}

This would output 1 2 3 4 5 and then terminate the loop.