In C programming, which of the following functions correctly computes the factorial of a non negative integer n using an iterative approach and returns the result as a long long?

Difficulty: Easy

Correct Answer: long long factorial(int n) { long long fact = 1; int i; for (i = 1; i <= n; i++) { fact *= i; } return fact; }

Explanation:


Introduction / Context:
Computing factorials is a standard exercise in learning loops and recursion. The factorial function n! is defined as the product of all positive integers from 1 to n, with the special case 0! equal to 1. An iterative implementation in C avoids the overhead and stack depth issues associated with recursion and is usually straightforward. This question asks you to identify the iterative version that correctly calculates n! and returns the value in a long long to allow for reasonably large results.


Given Data / Assumptions:

  • Input n is a non negative integer.
  • The factorial is defined as 1 for n equal to 0 and the product 1 * 2 * ... * n for n greater than 0.
  • The function should use a loop rather than recursion.
  • The return type is long long to reduce overflow risk for moderate n.


Concept / Approach:
An iterative factorial function maintains an accumulator variable initialised to 1 and multiplies it by each integer from 1 up to n. The accumulator must be declared with a type that can hold large products; long long is more suitable than int on many platforms. The loop should include both endpoints, so the counter runs from 1 through n inclusive. For n equal to 0, the loop body will not execute and the function will return the initial value 1, which matches the definition of 0!.


Step-by-Step Solution:
Step 1: Define factorial with signature long long factorial(int n). Step 2: Inside, declare long long fact = 1; to serve as the running product. Step 3: Use a for loop for (i = 1; i <= n; i++) fact *= i;. Step 4: After the loop, fact holds 1 * 2 * ... * n for n greater than zero, or 1 for n equal to zero, by virtue of the empty product rule. Step 5: Return fact as the result of the function.


Verification / Alternative check:
For n equals 5, the loop multiplies fact by 1, 2, 3, 4 and 5, yielding 120, which is the correct value of 5!. For n equals 0, the loop body does not execute, fact remains 1 and the function returns 1, as expected. You can test additional values such as 1 and 2 to confirm correctness. The long long type helps to avoid overflow for n up to around 20 on common implementations, although factorial growth will eventually exceed even this range.


Why Other Options Are Wrong:
Option B incorrectly returns 0 when n is 0 instead of 1, and although it uses recursion for n greater than 0, it misdefines the base case, so it does not compute factorial correctly. Option C declares fact as an int, misuses the loop and leaves the body empty, returning an undefined result. Option D simply returns n++ which does not compute factorial and uses post increment in a confusing way.


Common Pitfalls:
Students sometimes forget to handle the n equals 0 case correctly or write loops from 0 to n, which multiplies by 0 and produces 0 for all n greater than or equal to 0. Another pitfall is choosing an inappropriate type for the accumulator, causing silent overflow. Using long long and a loop from 1 to n as shown is a robust template for iterative factorial computation in most teaching examples.


Final Answer:
The correct iterative factorial uses a long long accumulator initialised to 1 and a loop that multiplies by each i from 1 through n, then returns the resulting product.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion