pow function in C

The pow function in C computes x raised to the power y (xy). A domain error occurs if x is finite and negative and y is finite and not an integer value. A domain error may occur if x is zero and y is zero. A range error occurs if the magnitude of nonzero finite x is too large or too near zero, depending on y. A domain error or pole error may occur if x is zero and y is less than zero.

It is declared in math.h and takes two arguments (base value and power value) in the form of double and returns the value of type double. The first argument is a base value and the second argument is a power raised to the base value.

 

Syntax of pow function in C:

//Syntax of pow() function in c

double pow( double x, double y);

Parameters of pow function in C:

x => ‘x’ represents the base value, whose power needs to be calculated. Its type is “double”.

y => ‘y’ represents the exponent value. Its type is also “double”.

 

Return value pow function in C:

If no errors occur, the pow function returns (xy).

 

Example,

Input: 3.0, 4.0
Output: 81

Explanation: 
pow(3.0, 4.0) executes 3.0 raised to
the power 4.0, which equals 81

 

You can also use the powf() function to work specifically with float and powl() to work with long double type. See the below syntax.

//other pow functions

float powf( float base, float exponent );   (1)	(since C99)

long double powl( long double base, long double exponent ); (2)	(since C99)

 

C program to understand the working of pow function:

Consider the below code where I am passing base and exponent values in the pow function. You can see the output of this function with entered input.

#include <stdio.h>
#include <math.h>

int main()
{
    double base, exponent, result;

    printf("Enter the base number: ");
    scanf("%lf", &base);

    printf("Enter the exponent raised: ");
    scanf("%lf",&exponent);

    result = pow(base,exponent);

    printf("%f^%f = %f", base, exponent, result);

    return 0;
}

Output:

Enter the base number: 3
Enter the exponent raised: 4
3.000000^4.000000 = 81.000000

 

Recommended Post: