Data Structure Questions and Answers-Sum of Digits of a Number using Recursion

Click on any option to know the CORRECT ANSWERS

 Question 1
Which of the following methods can be used to find the sum of digits of a number?
 A Recursion B Iteration C Greedy algorithm D Both recursion and iteration

Question 1 Explanation:
Both recursion and iteration can be used to find the sum of digits of a number.

 Question 2
What can be the maximum sum of digits for a 4 digit number?
 A 1 B 16 C 36 D none of the mentioned

Question 2 Explanation:
The sum of digits will be maximum when all the digits are 9. Thus, the sum will be maximum for the number 9999, which is 36.

 Question 3
What can be the minimum sum of digits for a 4 digit number?
 A 0 B 1 C 16 D 36

Question 3 Explanation:
The sum of digits will be minimum for the number 1000 and the sum is 1.

 Question 4
Consider the following iterative implementation to find the sum of digits of a number:

#include<stdio.h> int sum....of....digits(int n) { int sm = 0; while(n != 0) { .....; n /= 10; } return sm; } int main() { int n = 1234; int ans = sum....of....digits(n); printf("%d", ans); return 0; }

Which of the following lines should be inserted to complete the above code?

 A sm += n B sm += n%10 C sm += n-10 D sm += n/10

Question 4 Explanation:
The line "sm += n % 10" adds the last digit(LSB) of the number to the current sum. Thus, the line "sm += n%10" should be added to complete the above code.

 Question 5
What is the output of the following code?

#include<stdio.h> int sum....of....digits(int n) { int sm = 0; while(n != 0) { sm += n%10; n /= 10; } return sm; } int main() { int n = 1234; int ans = sum....of....digits(n); printf("%d", ans); return 0; }
 A 1 B 3 C 7 D 10