*Has to be coded in c using Dev c++. Must use printf and scanf statements*
1. A 5 digit positive integer is entered through the
keyboard. Write a recursive function to
calculate the sum of the digits of the number entered. (50 points -
10pts for commenting
and 40pts for successful execution)
#include <stdio.h>
/* Function declaration */
int digitsSum(int value);
/* Main Body */
int main()
{
int number, sum;
printf("Enter any number to find its sum of digits: ");
scanf("%d", &number);
sum = digitsSum(number); /*Function Calling*/
printf("Sum of digits of %d = %d", number, sum); /*Displaying
output*/
return 0;
}
/**
* Recursive function to find sum of digits of a number */
int digitsSum(int num)
{
// Condition to check finite number value
if(num == 0)
return 0;
return ((num % 10) + digitsSum(num / 10)); /*returns
integer value to the function return parameter sum*/
}
Output:
Get Answers For Free
Most questions answered within 1 hours.