Quick code to calculate exponentiation in C++

As suggested in the video, i built a code to calculate power operations.
using namespace std;

int main(int argc, char** argv) {
int a,b;
double c;
cout <<“Insert the base and the exponent”;
cin >>a>>b;
cout << “the result is”<<c<<endl;
return 0;
besides the string elements of the code, is this the correct way or there is a more pratical/efficient way to write it in C++?

Nice work @tcancian.
Using the pow function is the most common way to go. It’s not always the most efficient but it works for pretty much any situation and is easy to read.
There is always more than one way to solve a problem though and some may be more efficient under certain conditions. However, these solutions tend to be either; less readable or very niche in their application. Bit shifting would be a prime example, where it’s a lot quicker than using the pow function but only works if the base is 2.

Privacy & Terms