Next: Exercises
Up: The while statement
Previous: Multiple Choice Questions
int fact, i; fact = 1; i = 2; while (i <= 6) fact *= i; i++; cout << fact;If the purpose of this segment of C++ was to output the value of
1*2*3*4*5*6
how should it be changed?int i; while (i < 10) { cout << i << endl; i++; }To what should
i
be initialised so that the loop would be traversed 10
times? In this case what would be printed out? How would you change the
body of the loop so that with the same initialisation the numbers 1 to
10 would be printed? If the body of the loop was kept as it is above
how should the initialisation and the condition be changed so that the
numbers 1 to 10 are printed out?while
statement to print n
asterisks at the beginning of a new line.while
statement to evaluate n!
,
i.e. 1*2*3*...*n
, 0! is 1.