Convert the following while loop into a for loop.

Code:

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

Answer: The while loop can be converted to a for loop by initializing i, specifying the loop condition, and updating i in the for statement. Here’s the converted for loop:

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

Both the while loop and the for loop will output the same result: 1 2 3 4 5.