Difficulty: Medium
Correct Answer: #include
Explanation:
Introduction / Context:
Strong numbers are a specific type of special number used in programming exercises to practise loops, digit extraction and factorial computations. A number is called strong if it equals the sum of the factorials of its decimal digits. For example, 145 is strong because 1! + 4! + 5! equals 145. This question asks you to pick the C program fragment that correctly implements this definition for any input n and prints whether it is a strong number.
Given Data / Assumptions:
Concept / Approach:
To test whether n is strong, you must separate its digits using division and modulus operations, compute the factorial of each digit, sum these factorials and compare the sum with the original value. A helper function fact can compute factorials of digits. You must preserve the original n in a temporary variable for comparison, because the digit extraction loop will change temp. The check sum == n then determines whether the number is strong.
Step-by-Step Solution:
Step 1: Read n and copy it into temp so that temp can be reduced digit by digit.
Step 2: While temp is greater than zero, compute digit = temp % 10 to get the least significant digit.
Step 3: Call fact(digit) to compute the factorial of that digit and add it to sum.
Step 4: Divide temp by 10 to remove the processed digit and repeat until all digits have been handled.
Step 5: After the loop, compare sum with n; if equal, print "Strong number", otherwise print "Not a strong number".
Verification / Alternative check:
For n equals 145, the digits are 1, 4 and 5. The program computes 1! equals 1, 4! equals 24 and 5! equals 120, summing to 145. Thus sum equals n and the program prints "Strong number". For a non strong number such as 123, the sum of digit factorials 1! + 2! + 3! equals 9, which is not 123, so the program prints "Not a strong number". Testing with these values confirms that the logic matches the mathematical definition.
Why Other Options Are Wrong:
Option B simply tests whether n is even, which has no connection to the definition of strong numbers. Option C sums numbers from n down to 1 and compares the result to 145, which is unrelated to digit factorials. Option D prints a fixed message without actually performing any check, so it cannot distinguish strong numbers from non strong numbers.
Common Pitfalls:
Common mistakes include overwriting n instead of copying it to a temporary variable, which prevents correct comparison at the end, and computing factorials incorrectly or forgetting to reset sum before accumulation. Another pitfall is to hardcode the value 145 instead of generalising the test to any n. The correct approach always works digit by digit and compares the resulting sum back to the original input.
Final Answer:
The correct strong number check extracts each digit of n, adds the factorial of that digit to a running sum and then reports "Strong number" if and only if the final sum equals n.
Discussion & Comments