pow() Function Example in C Programming

In this source code example, we will see how to use the pow() function in C programming with an example.

pow() Function Overview

The pow() function in C calculates the power of one number raised to another. Specifically, it computes x^y where x is the base and y is the exponent. This function is paramount in various scientific, engineering, and mathematical computations and applications. The function can be found within the math.h library. 

Key Points: 

- To utilize the pow() function, ensure you include the math.h header. 

- The function receives two arguments, the base and the exponent. 

- Both the base and the exponent can be any real number. 

- Remember to link the math library using the -lm flag during compilation.

Source Code Example

#include <stdio.h>
#include <math.h>  // Required for pow()

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

    // Prompt the user for base and exponent
    printf("Enter the base: ");
    scanf("%lf", &base);
    printf("Enter the exponent: ");
    scanf("%lf", &exponent);

    // Compute the result using pow() function
    result = pow(base, exponent);

    // Display the computed result
    printf("%.2lf raised to the power of %.2lf is: %.2lf\n", base, exponent, result);

    return 0;
}

Output

Enter the base: 2
Enter the exponent: 3
2.00 raised to the power of 3.00 is: 8.00

Explanation

1. We start by incorporating the necessary header files: stdio.h for standard I/O functions and math.h to use the pow() function.

2. Inside the main() function, we request the base and exponent values from the user.

3. The pow() function then computes the result by raising the base to the power of the exponent.

4. The final result is printed to the console.


Comments