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?
#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;
}
A. sm += n
B. sm += n%10
C. sm += n-10
D. sm += n/10
Answer: Option B
Join The Discussion