Answer to Question #296800 in C++ for Roe

Question #296800

Write a program c++ that has a class MathUtils which has method that perform various mathematical functions using recursion.



class MathUtils


{


public:


int factorial ( int n)


{


//to be implemented


}


int power ( int base, int exp)


{


// to be implemented


}


void triple (int[] a)


{


//to be implemented


}


}



a. The factorial method returns the factorial of the number given. Implement factorial using recursion.


b. The power method raises base to the power exp. Implement power using recursion.


c. The triple method triples every element in the given array. Implement triple using recursion, adding a helper method if necessary.



Note:


No global decelerations


Test run in main

1
Expert's answer
2022-02-12T08:22:22-0500
#include<iostream>
using namespace std;


class MathUtils
{
public:
int factorial ( int n)
{
//to be implemented
if(n > 1)
    return n * factorial(n - 1);
  else
    return 1;


}
int power ( int base, int exp)


{
// to be implemented


    if (exp != 0)
        return (base* Power(base, exp-1));
    else
        return 1;
}
void triple (int[] a)
{
//to be implemented
  {
    helper(a, 0);
  }
    void helper (int[]a, int[k])
    while (k<a.length()-1)
            helper(a, k++)
}


int main() {


  int n;
  int Power(int, int);


  cout << "Enter a positive integer: ";
  cin >> n;


  cout << "Factorial of " << n << " = " << factorial(n);


  return 0;
  int base, exp, result;


    cout << "Enter base number: ";
    cin >> base;


    cout << "Enter power number(positive integer): ";
    cin >> exp;


    result = Power(base, exp);
    cout << base << "^" << exp << " = " << result;


    return 0;
    int[] a = {1, 2, 3};
    int helper;
    cout << "The Tripple of" << a << " = " << helper(a);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog