PLEASE HELP C++!! Write a program with a recursive function to output the sum of
ID: 3936591 • Letter: P
Question
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; }
Explanation / Answer
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
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.