C pow function

The C pow() function takes the base and exponent arguments to calculate the power of a specified value or number. For example, if x is the base value and 2 is the exponent, then pow(x, 2) = x².

C pow() function syntax

The syntax of the math pow() function is shown below.

double pow(double base, double Exponent);

Parameters

From the above syntax, we must specify the base value and the exponent value or power.

  • Base: Specify the number whose power is to be calculated.
  • Exponent: Power value. For example, base = 2 and exponent = 3 means 23.

Return Value

The pow() function calculates the base raised to the power of an exponent (baseexponent) and returns the floating-point number (double) as the output.

Header File Required

To use the pow() function, we must use the <math.h> header file.

#include<math.h>

NOTE: When you pass other data types, they will internally convert to double and apply the pow() function to operate.

C pow function Example

The pow() function is used to return the Power of the given number. In this program, we will find the power of both positive and negative values and display the output using this method and the printf statement.

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

int main()
{
  int result1; 
  double result2, result3, result4, result5;
   
  result1 = pow(5, 3);
  printf("\n The Final result of %d Power %d = %d ", 5, 3, result1);
  
  result2 = pow(2, 0);
  printf("\n The Final result of %d Power %d = %f ", 2, 0, result2);
  
  result3 = pow(0, 2);
  printf("\n The Final result of %d Power %d = %f ", 0, 2, result3);
  
  result4 = pow(-2, 3);
  printf("\n The Final result of %d Power %d = %f ", -2, 3, result4);
  
  result5 = pow(3, -4);
  printf("\n The Final result of %d Power %d = %f ", 3, -4, result5);
  
  return 0;
}
C pow function to find power of a number

Integer Base and Power

This program asks the user to enter the integer type base and exponent values. And then, math pow will calculate the power of user-specified values.

As we mentioned earlier, the C pow() function returns a double value; we must explicitly convert the double data type to an integer.

#include <stdio.h>
#include <math.h>
int main()
{
int result, base, exponent;
printf("Please Enter the Base and Exponent Values : ");
scanf("%d %d", &base, &exponent);

result = (int)pow(base, exponent);
printf("\nThe Final result of %d Power %d = %d ", base, exponent, result);

return 0;
}
Please Enter the Base and Exponent Values : 2 5
The Final result of 2 Power 5 = 32

Either you can follow the above approach or write the code below:

double result = pow(base, exponent);

Don’t use the code below; the implicit conversion creates problems for the large values.

int result = pow(base, exponent);

C pow() function with Negative Parameter Values

Negative Base Value and Positive Integer Exponent

In the following example, we use the pow() function with a negative base value of integer and double types. However, the exponent or power value is a positive integer. In such a case, the pow() function calculates the power and returns the value as a positive number.

#include <stdio.h>
#include <math.h>
int main()
{
printf("%.2f", pow(-2, 4));
printf("\n%.2f", pow(-2.5, 2));
return 0;
}
16.00
6.25

Negative Base Value and Positive Double-type Exponent

This time, we use the same negative double and integer type base values. However, we use the positive floating-point numbers as the exponent. In such a case, the pow() function returns the negative not a number (-nan).

#include <stdio.h>
#include <math.h>
int main()
{
printf("%.2f", pow(-3, 2.5));
printf("\n%.2f", pow(-5.5, 3.2));
return 0;
}
-nan
-nan

Negative Base Value and Negative Exponent

Here, we set the negative base and exponent (power) value, with a combination of integer and floating-point numbers. As long as the exponent value is an integer type, the pow() function calculates the power. However, if the exponent is a negative floating-point number, it returns -nan.

#include <stdio.h>
#include <math.h>
int main()
{
printf("%.2f", pow(-10, -2));
printf("\n%.2f", pow(-2.5, -2));
printf("\n%.2f", pow(-20, -3.5));
printf("\n%.2f", pow(-15.2, -4.5));
return 0;
}
0.01
0.16
-nan
-nan

Using Zero as the C pow() function Base or Power

The following examples show possible pow() function options with zeros and their outcomes.

  • Zero Base: Zero raised to the power of any value becomes 0. For instance, 0n = 0, so pow(0, 10) = 0.
  • Zero Exponent: If we use the pow() function with a proper base value and set the exponent to 0, it returns 1 as the output. For instance, X0 = 1 and pow(5, 0) = 1.
  • Zero Base and Exponent: If we pass zero as the base and exponent value to the pow () function, the function returns 1 as the output. For instance, pow(0, 0) = 1.00.
#include <stdio.h>
#include <math.h>
int main()
{
printf("Base = 0 and Power = 5: %.2f", pow(0, 5));
printf("\nBase = 10 and Power = 0: %.2f", pow(10, 0));
printf("\nBase = 0 and Power = 0: %.2f", pow(0, 0));
return 0;
}
Base = 0 and Power = 5: 0.00
Base = 10 and Power = 0: 1.00
Base = 0 and Power = 0: 1.00

Using C pow() function with float and long data types

In the following example, we use the pow() function on double, float, and long data types.

#include <stdio.h>
#include <math.h>
int main()
{
double x = 3.5, y = 4;
printf("%.2f", pow(x, y));

float a = 10.5f, b = 3.2f;
printf("\n%.2f", pow(a, b));

long double m = 100.25L, n = 5.5L;
printf("\n%.2Lf", pow(m, n));
return 0;
}
150.06
1852.70
101382756968.87

TIP: Experiment with the combination of positive and negative values of the data type.

Real-time examples

Square root of a number using the C pow() function

We can use the pow() function to calculate the square root of a positive number. However, if you use the technique mentioned below for the negative number (-36), it returns -nan as the output. Please use the sqrt() function.

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

int main() {
double result = pow(36, 0.5);
printf("%.2f\n", result);
}
6.00

Banking Application

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

int main() {
double principal = 500000;
double interestRate = 0.08;
int years = 2;

double amount = principal * pow((1 + interestRate / 12), 12 * years);

printf("Final Amount = %.2f\n", amount);

return 0;
}
Final Amount = 586443.97

Is pow(x, 2) slower than x * x?

When we want to calculate a number multiplied by itself, it is safer and faster to use the arithmetic operator than the pow() function. Similarly, n * n * n is faster compared to pow(n, 3).