C++ Program to Find Power of a Number

  • Write a C++ Program to calculate power of a number using pow function.
  • How to find power of a number using using multiplication.

C++ Program to calculate power of a number using loop

#include <iostream>

using namespace std;
 
int main(){
    int base, exp, i, result = 1;
    
 cout << "Enter base and exponent\n";
    cin >> base >> exp;
    
    // Calculate base^exponent by repetitively multiplying base
    for(i = 0; i < exp; i++){
        result = result * base;
    }
     
    cout << base << "^" << exp << " = " << result;
    
    return 0;
}
Output
Enter base and exponent
3 4
3^4 = 81

C++ Program to calculate power of a number using pow function

#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);
    return 0;
}
Output
Enter base and exponent
5 3
5^3 = 125