Emily is writing a function to calculate the factorial of a number using a loop. Which loop structure would be the most appropriate for this task?
- Do-While Loop
- For Loop
- Switch Statement
- While Loop
Emily should use a For Loop to calculate the factorial of a number. A For Loop is well-suited for tasks where you know in advance how many iterations are required, as is the case with factorial calculations. She can initialize the loop variable, set the loop condition to run until the variable reaches 1, and decrement the variable in each iteration to multiply the numbers in a descending order.
Loading...
Related Quiz
- Global variables, when declared outside all functions, have ______ storage duration by default.
- To check for possible errors or failures in file operations, you should check the _______.
- In a function that needs to return multiple values, which method of parameter passing might be employed to facilitate this without using data structures like tuples or pairs?
- In a large-scale C++ project, two classes, Logger and FileHandler, are tightly coupled, sharing a lot of private data between them using friendship. How might you refactor these classes to reduce coupling without losing functionality?
- In C++, an abstract class can have a mix of abstract as well as _______ methods.