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);.
An institute at the State level for training of State department of agriculture people is
Whiptail disease of cauliflower occurred due to the deficiency of which of the following nutrient?
Which nutrient helps in preventing Microcytic Anaemia?
The program used in the digitization technique of geoinformatics:
Tungro disease of rice is caused by
Biological control of apple woolly aphid is _____
Akoichi disease in rice is due to toxicity of
The element is found highest in earth crust is:
What is the maximum limit of chemically untreated conventionally grown raw material that can be used for making compost in an organic mushroom productio...
Soil physical property which cannot be changed of altered is: