Implement a math class for practicing recursive functions Th
Solution
math.h
#ifndef MATH_H
#define MATH_H
class math{
public:
int sum( int n );
int sum_r( int n );
int power( int n, int m );
int power_r( int n, int m );
};
#endif
math.cpp
#include \"math.h\"
int math::sum( int n ){
int result = 0;
for(int i = 1; i <= n; i++){
result+= i;
}
return result;
}
int math::sum_r( int n ){
if( n == 1 ){ return 1;}
return n + math::sum_r(n-1);
}
int math::power( int n, int m ){
int result = 1;
for(int i = 1; i <= m; i++){
result*= n;
}
return result;
}
int math::power_r( int n, int m ){
if( m == 0 ){ return 1; }
return n*math::power_r(n, m-1);
}
driver.cpp
#include \"math.h\"
#include <iostream>
using namespace std;
int main(){
math c;
cout << c.sum(5) << \" \" << c.sum_r(5) << endl;
cout << c.power(5,2) << \" \" << c.power_r(5,2) << endl;
return 0;
}
compile as g++ -o a driver.cpp math.cpp

