96.2k views
0 votes
Rewrite the given C code shown below using the for repetition structure and the do .. while repetition control structure. #include int main() { int counter =1; int fact =1; while (counter <=6) { fact = fact ∗ counter; counter = counter +1 ; } printf("%d", fact); return 0 ; }

User Rennat
by
8.2k points

1 Answer

0 votes

Final answer:

The question requires rewriting C code that calculates the factorial of 6 using a for loop and a do..while loop. Both rewritten versions of the code are provided, illustrating how each control structure accomplishes the same task.

Step-by-step explanation:

The student's question relates to rewriting a given piece of C code using different repetition control structures, specifically the for loop and the do..while loop. The original code calculates the factorial of 6 using a while loop. Below are the rewritten versions of the code using the requested structures.

Using a for loop:

#include
int main() {
int fact = 1;
for (int counter = 1; counter <= 6; counter++) {
fact = fact * counter;
}
printf("%d", fact);
return 0;
}

Using a do..while loop:

#include
int main() {
int counter = 1;
int fact = 1;
do {
fact = fact * counter;
counter = counter + 1;
} while (counter <= 6);
printf("%d", fact);
return 0;
}

Both structures perform the same task of calculating the factorial, with syntax differences being the main variance between them.

User David Plumpton
by
8.6k points