Print Friendly and PDF
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. The function integerPower should use for or while to control the calculation. Do not use any math library functions.
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. The function integerPower should use for or while to control the calculation. Do not use any math library functions.

Solution :
#include <iostream>
using namespace std;
int integerPower( int, int );
int main()
{
int exp, base;
cout << "Enter base and exponent: ";
cin >> base >> exp;
cout << base << " to the power " << exp << " is: " << integerPower( base, exp ) << endl;
return 0;
}
int integerPower( int b, int e )
{
int product = 1;
for ( int i = 1; i <= e; ++i )
product *= b;
return product;
}
Output 
Enter base and exponent: 6 2
6 to the power 2 is: 36
zubairsaif

Zubair saif

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

Post A Comment:

0 comments: