Question
Consider the following C code snippet designed to
calculate the sum of digits of a non-negative integer:   #include   int sumDigits(int n) {     if (n == 0) {       return 0;     }     // Error in recursive step     return sumDigits(n / 10);   }   int main() {     printf("%d\n", sumDigits(123));     return 0;   }   What is the error in the sumDigits function above, and how should it be corrected to correctly calculate the sum of digits of a non-negative integer?Solution
The current implementation of sumDigits(n) for n > 0 simply calls sumDigits(n / 10). This means it effectively truncates the last digit (n % 10) and only processes the remaining digits. For example, sumDigits(123) would call sumDigits(12), then sumDigits(1), then sumDigits(0), which returns 0. The final output would be 0, which is incorrect. To correctly sum the digits, the current digit (n % 10) must be added to the sum of the remaining digits. The corrected recursive step should be return (n % 10) + sumDigits(n / 10);.
(72 × 52 + 1555 )/(79+60) = 2000 ÷ ?
16 × ? + 36% of 250 = 410
Simplify: 0.125 + 0.375 + 0.5 and express the answer as a fraction in simplest form.
Determine the simplified value of the given mathematical expression.
12% of 450 + 16% of 1500 = ?
What will come in the place of question mark (?) in the given expression?Â
48 ÷ 4 × (3 + 5) – 6² + 10 = ?
{(5/8) + (4/5)} × (?/19) = 33
If a³ - b³ = (a - b)(a² + ab + b²), find a³ - b³ when a = 10 and b = 4.
(1296)Â -3/4Â = ?
2.4 of 7.2 of 1/57.6 of 4200 = ?