Calculate the Power of a Number

Program to calculate base raised to the power of exponent

BeginnerTopic: Loop Programs
Back

C++ Calculate the Power of a Number Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double base, exponent, result;
    
    cout << "Enter base: ";
    cin >> base;
    
    cout << "Enter exponent: ";
    cin >> exponent;
    
    result = pow(base, exponent);
    
    cout << base << " raised to the power " << exponent << " = " << result << endl;
    
    return 0;
}
Output
Enter base: 2
Enter exponent: 8
2 raised to the power 8 = 256

Understanding Calculate the Power of a Number

This program calculates base^exponent using the pow() function from cmath library. The pow() function takes two arguments: base and exponent, and returns the result. We use double data type to handle both integer and floating-point exponents.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Table of Contents