PLEASE HELP C Write a program with a recursive function to o
PLEASE HELP C++!!
Write a program with a recursive function to output the sum of the digits in a positive integer.
Complete provided function prototype in your code, then use the provided driver program to test your code. You should get the same output.
#include <iostream>
using namespace std;
int addDigit (int n);
int main ( )
{
cout << \"sum of the digits in the number 2017 is\" << addDigit (2017)<< endl;
cout << endl;
return 0;
}
PLEASE HELP C++!!
Write a program with a recursive function to output the sum of the digits in a positive integer.
Complete provided function prototype in your code, then use the provided driver program to test your code. You should get the same output.
#include <iostream>
using namespace std;
int addDigit (int n);
int main ( )
{
cout << \"sum of the digits in the number 2017 is\" << addDigit (2017)<< endl;
cout << endl;
return 0;
}
Write a program with a recursive function to output the sum of the digits in a positive integer.
Complete provided function prototype in your code, then use the provided driver program to test your code. You should get the same output.
#include <iostream>
Write a program with a recursive function to output the sum of the digits in a positive integer.
Complete provided function prototype in your code, then use the provided driver program to test your code. You should get the same output.
#include <iostream>
using namespace std;
int addDigit (int n);
int main ( )
{
cout << \"sum of the digits in the number 2017 is\" << addDigit (2017)<< endl;
cout << endl;
return 0;
}
Solution
Answer:
C++ Code :
#include <iostream>
using namespace std;
int addDigit (int n);
int addDigit (int n)
{
if(n<=0)
return 0;
else
return (n%10) + addDigit(n/10);
}
int main ( )
{
cout << \"sum of the digits in the number 2017 is \" << addDigit (2017)<< endl;
cout << endl;
return 0;
}
Output :
sum of the digits in the number 2017 is 10

