C++ Program to Find Power of a Number

C++ Program to calculate power of a number using pow function and 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

Recommended Posts
C++ Program to Find Power of Number using Recursion
C++ Program to Display Factors of a Number
C++ Program to Check for Armstrong Number
C++ Program to Reverse Digits of a Number
C++ Program to Find GCD of Two Numbers
C++ Program to Make a Simple Calculator
C++ Program to Display Fibonacci Series using Loop and Recursion
C++ Program to Print Multiplication Table of a Number
C++ Program to Check whether a string is Palindrome or Not
All C++ Programs