LiAM - Exponent Challenge

I’m very new to coding in C++ which I intend to learn so I had a bit of a Google and mashed together three bits of code. One was just simply to work out an exponent using pow(), but the answer was given in scientific notation so I found that adding << fixed before the pow would give a decimal answer but it had an unsightly bunch of zeros after the decimal point so I found out that adding << setprecision() from #iomanip would let you control the amount of decimal places to display. Here is the resulting code:

#include <iomanip>
#include <iostream>
#include <cmath>
 
using namespace std;
 
int main() {
    int base, exp;
 
    cout << "Enter base and exponent\n";
    cin >> base >> exp;
 
    cout << base << "^" << exp << " = " << pow(base, exp);
    cout << base << "^" << exp << " = " << fixed << setprecision(1) << pow(base, exp);
    return 0;
}

This allows you to find any power by typing in the base number a space and then the exponent.

Privacy & Terms