Home » C Program » C program to calculate the power using the POW method

C program to calculate the power using the POW method

Last updated:

Support this post with a reaction:

5,355 views

The power of a number can be calculated using the built-in pow() function. This function is available in the math.h header file and is used to calculate the value of a base raised to a given exponent.

For example, if we want to calculate 3 raised to the power of 4, the mathematical expression is 3^4, and the result is 81.

What Is the Power of a Number?

The power of a number represents how many times a number is multiplied by itself.

It consists of two main components:

  • Base: The number that is multiplied repeatedly.
  • Exponent: The number that specifies how many times the base is used as a factor.

For example:

43=4×4×4=64 4^3 = 4 \times 4 \times 4 = 64

Here:

  • Base = 4
  • Exponent = 3
  • Result = 64

More examples:

ExpressionCalculationResult
2^32 × 2 × 28
3^43 × 3 × 3 × 381
5^25 × 525
10^310 × 10 × 101000
2^52 × 2 × 2 × 2 × 232

What Is the pow() Function in C?

The pow() function in C is a mathematical library function used to calculate the power of a number.

It is declared in the math.h header file.

Syntax:

pow(base, exponent);

Parameters:

  • base: Represents the number whose power needs to be calculated.
  • exponent: Represents the power to which the base is raised.

Return value:

The pow() function returns a double value representing the calculated power.

Example:

pow(2, 3);

The above function calculates:

23=2×2×2=8 2^3 = 2 \times 2 \times 2 = 8

Therefore, the function returns 8.0.

C Program to Calculate Power Using pow() Function

The following C program takes the base and exponent as input from the user and calculates the power using the pow() function.


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

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

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

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

    result = pow(base, exponent);

    printf("%.2lf raised to the power %.2lf = %.2lf",
           base, exponent, result);

    return 0;
}

Output:

Enter the base: 3
Enter the exponent: 4
3.00 raised to the power 4.00 = 81.00

Explanation of the Power Program in C

  1. Include the stdio.h header file for input and output operations.
  2. Include the math.h header file to use the pow() mathematical function.
  3. Declare three variables: base, exponent, and result to store the input values and calculated power.
  4. Read the base value from the user.
  5. Read the exponent value from the user.
  6. Call the pow() function by passing the base and exponent as arguments.
  7. Store the returned result in the result variable.
  8. Display the calculated power using the printf() function.
  9. Terminate the program using return 0.