Advertisement
BojidarDosev

Recursion: Insert num x and pow y and receive output

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