Using C Ask the user for a base float and a positive exponen
Using C++:
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
Power.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float b;
int e;
cout << \"Please enter the base: \";
cin >> b;
cout<<\"Please enter the exponent: \";
cin >> e;
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
float result=1;
for(int i=0; i<e; i++){
result = result * b;
}
cout<<\"The answer is \"<<result<<endl;
return 0;
}
Output:
sh-4.3$ g++ -std=c++11 -o main *.cpp
sh-4.3$ main
Please enter the base: 2
Please enter the exponent: 3
The answer is 8.00
sh-4.3$
