(Exponentiation) Write a function integerPower(base, exponent) that returns the value of
base exponent
For example
integerPower(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer and that base is an integer. Do not use any math library functions.
base exponent
For example
integerPower(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer and that base is an integer. Do not use any math library functions.
Solution:
#include < iostream> using std::cout; using std::cin; int integerPower( int, int ); int base, exponent; int main() { cout << "Enter base and exponent: "; cin >> base >> exponent; cout << "Result: " << integerPower( base, exponent); return 0; } //integer power function int integerPower( int base, int exponent) { int result = 1; while( exponent > 0) { result *= base; exponent--; } return result; }
Post A Comment:
0 comments: