A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c/power-function-c-cpp/ below:

Power Function in C - GeeksforGeeks

Power Function in C

Last Updated : 30 Apr, 2025

Try it on GfG Practice

In C language, the pow() function is defined in the <math.h> header file and is used to calculate the exponent value of x raised to the power of y, i.e., xy. Basically, in C, the exponent value is calculated using the pow() function.

Example:

C
#include <stdio.h>
// Include math.h for the pow() function
#include <math.h>  

int main() {
    double base = 5, exponent = 3, result;

    // Calculate the result of base 
    //  raised to the power of exponent
    result = pow(base, exponent);

    // Output the result
    printf("%.0f raised to the power of %.0f is %.0f\n", base, exponent, result);

    return 0;
}

Output
5 raised to the power of 3 is 125
pow() in C

To use the pow() function in our program we need to include the <math.h> in our C program. The pow() function takes a double as input and returns a double as output. The pow() function has 3 different overloads that return values in double, float or long double based on the data type of input values.

Syntax C
//takes double as input and returns double
double pow(double base, double exponent);

//takes float as input and returns float
float pow(float base, float exponent);

//takes long double as 
// input and returns long double
long double pow(long double base, 
                long double exponent);
Parameters Return Value Examples Using pow() Function C
#include <stdio.h>
#include <math.h>

int main()
{
    //taking double as input
    double x = 6.176, y = 4.832;

    printf("%f raised to power of %f is %f\n", x, y, pow(x, y));
    
    //taking float as input
    float a = 3.14, b = 2.58;

    printf("%f raised to power of %f is %f\n", a, b, pow(a, b));
    
    //taking long double as input
    long double p = 2.1591, q = 2.8642;

    printf("%Lf raised to power of %Lf is %f\n", p, q, pow(p, q));
    

    return 0;
}

Output
6.176 raised to power of 4.832 is 6617.56
3.14 raised to power of 2.58 is 19.146
2.1591 raised to power of 2.8642 is 9.06617
pow() Function with Integers

The pow() function takes 'double' as the argument and returns a 'double' value. This function does not always work properly for integers. One such example is pow(5, 2). When assigned to an integer, it outputs 24 on some compilers and works fine for some other compilers. But pow(5, 2) without any assignment to an integer outputs 25. 

One another way can be using the round function to assign it to some integer type.

Example 1: C Program to demonstrate the behavior of the pow() function with integers. C
// C program to illustrate
// working with integers in
// power function
#include <math.h>
#include <stdio.h>

int main()
{
    int a, b;

    // Using typecasting for
    // integer result
    a = (int)(pow(5, 2) + 1e-9);
    b = round(pow(5,2));
    printf("%d \n%d", a, b);

    return 0;
}

Time Complexity: O(log(n))
Auxiliary Space: O(1)



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4