In: Computer Science
Write a function called integerPower(base, exponent) that returns the value of baseexponent
For example,
integerPower(3,4) = 3*3*3*3
An example of input/output dialog is shown below:
Enter Integer Base: 5 Enter Integer Exponent: 3 5 raised to the power of 3 = 125 Enter 0 if you'd like to end this program. Enter 1 if you'd like to run this program again: 1 Enter Integer Base: 2 Enter Integer Exponent: 4 2 raised to the power of 4 = 16 Enter 0 if you'd like to end this program. Enter 1 if you'd like to run this program again: 0 Have a nice day!
#include<iostream>
using namespace std;
int integerPower(int base, int exponent)
{
int ans = 1;
int i;
for( i = 1 ; i <= exponent ; i++ )
ans *= base;
return ans;
}
int main()
{
while(1)
{
cout<<"Enter Integer Base: ";
int base;
cin>>base;
cout<<"Enter Integer Exponent: ";
int exponent;
cin>>exponent;
cout<<base<<" raised to the power of "<<exponent<<" = "<<integerPower(base, exponent)<<endl;
cout<<"\nEnter 0 if you'd like to end this program.";
cout<<"\nEnter 1 if you'd like to run this program again: ";
int choice;
cin>>choice;
// if user wants to quit
if( choice != 1 )
break;
cout<<endl;
}
}
Sample Output