* N O Ol 4..ul 84%-7:07 AM LTE For each of the following problems, write a function that solves the problem recursively. No for loops or while loops allowed! You should test each functiorn in your main method. You should just submit a single.cpp file. No separate compilation needed. Problem 1 Write a function that takes a string and returns true if that strina is a palindrome and false otherwise The function should look like this bool isPalindrome(const string& input) Problem 2 Write a function that takes an integer and returns the sum of its digits. For this function, you will have to make clever use of the / and % operators! Your function should look like this: int digitSum(int input){
Expert Answer
c++ code:
#include <iostream>
using namespace std;
bool ispalindrom( string str, int start, int end)
{
if(start == end)
{
return true;
}
if(str[start] != str[end])
{
return false;
}
if(start < end + 1)
{
return ispalindrom(str, start + 1, end – 1);
}
return true;
}
int digitsum(int n)
{
if(0<=n and n<=9)
{
return n;
}
else
{
return (n%10 + digitsum(n/10));
}
}
int func(int n)
{
if( n <= 1)
{
return (n);
}
else
{
return func( n – 1) + func( n – 2);
}
}
int waystoclimb(int nstairs)
{
return func(nstairs + 1);
}
int main()
{
string test1 = “akash”;
string test2 = “abcddcba”;
cout << ispalindrom(test1,0,test1.length() – 1) << “n”;
cout << ispalindrom(test2,0,test2.length() – 1) << “n”;
cout << “Sum of digits of 123 = ” << digitsum(123) << endl;
cout <<“Sum of digits of 43009 = ” << digitsum(43009) << endl;
cout <<“Sum of digits of 747384 = ” << digitsum(747384) << endl;
cout << “Sum of digits of 12345 = ” <<digitsum(12345) << endl;
cout << “For stairs = 4, n_ways = ” << waystoclimb(4) << endl;
cout << “For stairs = 1, n_ways = ” << waystoclimb(1) << endl;
cout << “For stairs = 2, n_ways = ” << waystoclimb(2) << endl;
return 0;
}
Sample Ouput:
0
1
Sum of digits of 123 = 6
Sum of digits of 43009 = 16
Sum of digits of 747384 = 33
Sum of digits of 12345 = 15
For stairs = 4, n_ways = 5
For stairs = 1, n_ways = 1
For stairs = 2, n_ways = 2