Ask the user for a base float and a positive exponent int Ca
Ask the user for a base (float) and a positive exponent (int). Calculate the base raised to the power of the exponent using a loop. For example:
Use the following format specifiers to get the precision to match the answers:
Solution
#include <iostream>
using namespace std;
int main()
{
float result =1;
int i;
float base;
int exponent;
cout<<\"\ Please enter the base:\";
cin>>base;
cout<<\"\ Please enter the exponent:\";
cin>>exponent;
for(i=1;i<=exponent;i++)
{
result=result*base; //result=base*base*base*...... exponent times
}
cout.setf(ios::fixed, ios::floatfield); //format specifier
cout.precision(2);
cout<<\"\ The answer is :\"<<result;
return 0;
}
output:
Please enter the base:2
Please enter the exponent:3
The answer is :8.00
