Calculate Power of a Number

Calculate Power of a Number in C++ (6 Programs)

BeginnerTopic: Advanced Number Programs
Back

C++ Calculate 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;
    
    cout << "Enter base: ";
    cin >> base;
    
    cout << "Enter exponent: ";
    cin >> exponent;
    
    // Method 1: Using pow() function
    double 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 Power of a Number

This program demonstrates 6 different methods to calculate power: using pow() function, using loops, using recursion, using bit manipulation, using logarithms, and using exponentiation by squaring.

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