Skip to content
doubtly

2 answers

Answer:

a = 1;
while (a <= 100) {
    printf("%d\n", a * a);
    a++;
}

A for loop has three parts: initialisation (a = 1), condition (a <= 100) and update (a++). To convert it to a while loop:

  • put the initialisation before the loop;
  • put the condition in while ( );
  • put the body and then the update inside the loop's braces.

The braces are essential. Without them, as in while (a<=100) printf(...); a++;, only the printf belongs to the loop, a never increases, and the loop prints 1 forever (an infinite loop).

Both versions print the squares 1, 4, 9, …, 10000. (The "%dn" in the question is meant to be "%d\n".)

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.