Tech Study

C++ Power Function | C++ pow()

Power in c++ helps us to find power of number given by user.

What is c++ power function

The C++ pow() function raise x to the power of y, given two number, base and exponent which is “xy”. It returns x raised to the power of y. Exponent value is determined using the pow() c++ function and the power is calculated.

Syntax for C++ pow() :

C++ Power Function syntax:

double pow(double x, double y);

C++ pow() Parameters:

  • x – It is the base value.
  • Y – It is a power value.

Return Value for c++ power function:

c++ pow() returns X raised to the power of y, as result.

Example of c++ power of a number

In this tutorial, you will learn to write pow c++ function to calculate the c++ power of 2 numbers using while & for loop.

we are using CodeBlocks compiler for debugging purposes. But you can use any C++ programming language compiler as per your availability.

Example 1:

Learn to calculate power in c++ using while loop.

#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    int base, exponent,power, i;
 
    //Reading base & exponent
    cout<<"Enter base: ";
    cin>>base;
    cout<<"Enter exponent: ";
    cin>>exponent;
 
    power = 1;
    i = 1;
    //caculatinh power of given number
    while(i <= exponent)
    {
        power = power * base;
        i++;
    }
    cout<<"Power of "<<base<<" is: " <<power;
 
    return 0;
}

Example 2:

Learn to calculate power in c++ using for loop.

#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    int base, exponent, i, power;
 
    //Reading base & exponent
    cout<<"Enter base: ";
    cin>>base;
    cout<<"Enter exponent: ";
    cin>>exponent;
 
   power = 1;
 
    //caculatinh power of given number using for loop
    for(i=1; i<=exponent; i++)
        power = power * base;
    cout<<"Power of "<<base<<" is: " <<power;
 
    return 0;
}

Result

Write_program_to_calculate_c++_power_using_while_&_for_loop
After executing, loop, the program returns with power of number entered by the user. After successful completion, it returns value 0.

TaggedWrite C program to calculate power using while & for loop

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more