Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int recursivePower(int a, int n) {
- return n ? a * recursivePower(a, n - 1) : 1 ; //If n = 0, returns 1; otherwise calls itself with one less power
- }
- int iterativePower(int a, int n) {
- int result = 1;
- for (int i = 0; i < n; i++) //multiplies 'a' n times. thus, a * a * a = a^3
- result *= a;
- return result;
- }
- int main() {
- int A, N;
- cout << "Enter an Integer: ";
- cin >> A;
- cout << "Enter the exponent: ";
- cin >> N;
- cout << "Recursive Power: " << recursivePower(A, N) << endl;
- cout << "Iterative Power: " << iterativePower(A, N) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement