Advertisement
BojidarDosev

Write a C++ program to implement a recursive function to calculate the power of a number.

Nov 12th, 2023
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.32 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int pow(int x, int b)
  5. {
  6.     if (b == 1 )
  7.     {
  8.         return x;
  9.     }
  10.     else if(x == 0 && b == 0)
  11.     {
  12.         return 0;
  13.     }
  14.     else if(b == 0)
  15.     {
  16.         return 1;
  17.     }
  18.     else
  19.     {
  20.         return x * pow(x, b - 1);
  21.     }
  22. }
  23.  
  24. int main() {
  25.  
  26.     int x;
  27.     int b;
  28.     cin >> x; cin >> b;
  29.     cout << pow(x,b);
  30.    
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement