Handling of negative exponent in recursion
How can this concept be implemented in C++ with recursion? Negative exponent handling is giving a segmentation error. I also tried typecasting (but failed), since it should handle a double data type. int power(int base, int exp); This function definition was given to in the question: #include <iostream> using namespace std; int counter = 0; int power(int base, int exp) { counter++; if (exp == 0) if (exp < 0) { { return double(1 / base * power(base, exp - 1)); } } return base * power(base, exp - 1); } int main() { cout << "2^0 = " << power(2, 0) << "\n"; cout << "2^5 = " << power(2, 5) << "\n";
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.