-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpow.cpp
More file actions
31 lines (26 loc) · 704 Bytes
/
Copy pathpow.cpp
File metadata and controls
31 lines (26 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* TODO: write a *recursive* function that takes two integers (a,b)
* and computes a^b (a to the b power). You can assume the exponent
* b is nonnegative. */
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int base, exponent;
cout << "Please enter a base: ";
cin >> base;
cout << "Please enter an exponent: ";
cin >> exponent;
int result = pow(base, exponent);
cout << result << endl;
return 0;
}
// vim:foldlevel=2
//To compute a^b, first compute a^(b-1), then multiply by a. return a * power(a, b - 1);
/*power(3, 2)
→ power(3, 1)
→ power(3, 0)
→ returns 1
→ returns 3 * 1 = 3
→ returns 3 * 3 = 9*/