Print Friendly and PDF
(Exponentiation) Write a function integerPower(base, exponent) that returns the value of baseexponent 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.
(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.
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;
}
 
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: