***All programs must be written in C and must be compilable in DEV C++ (if possi
ID: 3558859 • Letter: #
Question
***All programs must be written in C and must be compilable in DEV C++ (if possible)***
-Complete programs only
-Please run them before you submit them to make sure they work
-Incomplete programs that do not run will not be awarded points.
Thanks a bunch!!
QUESTION 1. (10 points) Write a recursive function called 'add digits' that takes an integer as a parameter and adds its digits. For example, if we call this function with the parameter n 1234, the returned value is 1+2+3+4 10. Include the "main' function in your code and show how the "main' function calls the function "add digits'. Make sure the function is recursive (it calls itself), and not iterativeExplanation / Answer
#include<stdio.h>
#include<conio.h>
int count(int x)
{
int sum = 0,r=0;
if(x==0)
return 0;
else
{
r = x%10;
x = x/10;
sum= r+count(x);
}
return sum;
}
int main()
{
int x;
scanf("%d",&x);
printf("%d",count(x));
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.