Exponent c++ – C++ Program to Find Power of a Number

Exponent c++: In the previous article, we have discussed C++ Program to Convert Decimal Number to HexaDecimal Number. In this article, we will see C++ Program to Find Power of a Number.

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

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

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

Are you in search of Sample C++ Program to understand the logic for fundamental C++ programming language concepts? Check out the list of C++ Sample Program ranging from basic to complex ones at our end.