Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program to demonstrate the recursive function power.
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- int power(int x, int n);
- //Precondition: n > = 0.
- //Returns x to the power n.
- int main( )
- {
- for (int n = 0; n < 4; n++)
- cout << "3 to the power " << n
- << " is " << power(3, n) << endl;
- return 0;
- }
- //uses iostream and cstdlib:
- int power(int x, int n)
- {
- if (n < 0)
- {
- cout << "Illegal argument to power.\n";
- exit(1);
- }
- if (n > 0)
- return ( power(x, n - 1) * x);
- else // n == 0
- return (1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement