What is the equivalent code of the following statement in WHILE LOOP format?
What is the equivalent code of the following statement in WHILE LOOP format?
for (a=1; a<=100; a++)
printf ("%d\n", a * a);What is the equivalent code of the following statement in WHILE LOOP format?
for (a=1; a<=100; a++)
printf ("%d\n", a * a);Best answer
a=1;
while (a<=100) {
printf ("%d\n", a * a);
a++;
}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:
while ( );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".)
Log in or create a free account to answer this question.