Home » C++ programming language

pow() function with example in C++

C++ pow() function: Here, we are going to learn about the pow() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 26, 2019

C++ pow() function

pow() function is a library function of cmath header (<math.h> in earlier versions), it is used to find the raise to the power, it accepts two arguments and returns the first argument to the power of the second argument.

Syntax of pow() function:

    pow(x, y);

Parameter(s): x, y – are the numbers to calculate x^y.

Return value: double – it returns double value that is calculate result of x to the power of y.

Example:

    Input:
    float x = 2;
    float y = 3;

    Function call:
    pow(x,y);

    Output:
    8

C++ code to demonstrate the example of pow() function

// C++ code to demonstrate the example of 
// pow() function

#include <iostream>
#include <cmath>
using namespace std;

// main code section
int main()
{
    float x;
    float y;
    
    //input the values
    cout<<"Enter the value of x: ";
    cin>>x;
    cout<<"Enter the value of y: ";
    cin>>y;
    
    // calculate the power
    float result = pow(x,y);
    cout<<x<<" to the power of "<<y<<" is = "<<result;
    cout<<endl;
    
    return 0;
}

Output

First run:
Enter the value of x: 10 
Enter the value of y: 5
10 to the power of 5 is = 100000

Second run:
Enter the value of x: 10.23
Enter the value of y: 1.5
10.23 to the power of 1.5 is = 32.72

Third run:
Enter the value of x: 10 
Enter the value of y: 0
10 to the power of 0 is = 1 

Fourth run:
Enter the value of x: 0
Enter the value of y: 1.5
0 to the power of 1.5 is = 0



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.