Ken H

Ken H

  • NA
  • 646
  • 361.7k

cube root in c++

Oct 9 2013 9:40 AM
Hello everyone,
       I want to achieve this cube root function, my code is as follows:

#include <iostream> 
#include <iomanip>
#include <cmath>
using namespace std;
class MyMath
{
public:

unsigned long int Square(int target,int k); 

unsigned long int Factorial(int n);

double SquareRoot(double n);

double CubeRoot(double n);

};


unsigned long int MyMath::Square(int target,int k)
{
  unsigned long int result=1;
  for (int i=1;i<=k;i++) result*=target;

  return result;
}


unsigned long int MyMath::Factorial(int n)
{
   if(n==1) return 1;
   else return Factorial(n-1)*n;
}


double MyMath::SquareRoot(double n)
{
   return sqrt((double)n);
}


double MyMath::CubeRoot(double n)
{
    /*return pow((double)n,3);*/
/*
   For example, enter 27 the cube root of the result returned after 3.
*/
}


int main()
{
        MyMath mm;
        int n;
        cout<<"please input a integer number:";

cin>>n;

cout<<n<<"="<<mm.Square(n,2)<<endl;

cout<<n<<"="<<mm.Square(n,3)<<endl;

cout<<n<<"="<<mm.Factorial(n)<<endl;

cout<<n<<"="<<mm.SquareRoot(n)<<endl;

//cout<<n<<"="<<mm.CubeRoot(n)<<endl;


      return 0;
}

Regards


Ken.


Thank you. :)


Answers (2)