Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Jul 9, 2024
Easy

Power Function in C

Table of contents
Learn to use AI Tools & ChatGPT to excel as Microsoft SDE
11 Jul, 2024 @ 01:30 PM
Speaker
Pranav Malik
SDE 2 @

Introduction

The power function also known as the pow() function is used to calculate the value x^y(x raised to the power y), where x is the base and y is the exponent. x and y are both variables of the type double. The type of result returned by pow() is double. 

Pow() Function in C

The pow() function is a built-in function in math.h header file. We must include the math.h header file in our application in order to utilize this function. In order to use pow(), the base value (x) and the exponent value (y) are both sent to pow(). 
The value x^y can be calculated using pow(x, y).

Also Read About, Sum of Digits in C, C Static Function

How to Use the Power Function in C?

To use the power function in C, you need to include the math.h header file. The power function is pow(base, exponent), where base is the base value and exponent is the exponent. It returns the result of raising the base to the power of the exponent. For example, pow(2, 3) returns 8.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Working of Power Function in C 

The pow() function accepts double values as parameters and returns a double value. However, the function does not always operate with integers. 
For example, if the value given by pow(5, 4) is assigned to an integer variable, the result may be 624 on certain compilers and 625 on others. This is because the value 5^4 (i.e. 625) might be saved as 624.999999 in certain compilers and 625.0000000001 in others. When these numbers are given to int, 624.999999 translates to 625, resulting in erroneous output. 
We can overcome this problem by adding 0.0000000001 (1^{-9} or 1e-9) to the output and explicitly typecasting it into an int. For example, (int) (pow(4, 3) + 1e-9) will always give the correct answer. You can learn more about Typecasting in C from this link.

You can also read about C dynamic array and  Short int in C Programming

Syntax for Pow() Function in C

The syntax of the pow() function is as follows:

double pow(double x, double y);

The pow() function takes two integer arguments x and y. 
x: The variable x represents the base value, whose power needs to be calculated. It is of the type double. 
y: The variable y represents the exponent value. It is of the double type.

Return Values for pow() Function in C 
The pow() function returns the value x^y(x raised to the power of y) where x and y are two variables of type double. The return type is double.

You can also read about the jump statement, Tribonacci Series

Must Read Passing Arrays to Function in C

Parameters for pow() in C

The pow() function in C takes two parameters: the base value (double) and the exponent value (double) to calculate the power.

Return Values for pow() Function in C

The pow() function returns the result of raising the base to the power of the exponent as a double precision floating point number.

Examples for pow() in C

Example 1

Let's take an example. To better understand the pow() function, consider the following scenario: The base value is an integer, and the power value is a double.

  • C

C

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

int main()
{
   int x = 5; // base
   double y = 6.1;  // power

   // using the pow() function
   double ans = pow(x, y);
  
   // printing the output
   printf("%f", ans);

   return 0;
}


Output

18353.420986

Explanation

In the above example, the base was of type int while the power was of type double. We used pow() on these variables to get our output.

Example 2

Let's look at an example to see how we can use the pow function to simply get the square root of an integer. A number's square root is just that number raised to the power of ½ or 0.5.

  • C

C

#include <math.h>
#include <stdio.h>
int main()
{
printf("%f", pow(9, 0.50));

return 0;
}

Output:

3.000000

Explanation

pow() can be used to calculate the roots of positive numbers. 

Example 3

Let's look at an example to see how we can pass negative values with the power of type int.

  • C

C

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

int main()
{
 printf("%f \n", pow(-2, -7));

 printf("%f", pow(2, -7));
   return 0;
}


Output

-0.007812 
0.125000

Explanation

The power's value in pow() can be negative.

Example 4

Let's look at an example of passing negative values with the power of type double.

  • C

C

#include <math.h>
#include <stdio.h>
int main()
{
 printf("%f \n", pow(-3.5, 11.2));
  
 printf("%f", pow(-5.5, -7.2));
   return 0;
}


Output

-nan
-nan

Explanation

If the base is negative and the power is of type double, the output will be -nan as it is an undefined entity. If we calculate the root of a negative number using pow(), we will get -nan as the output, which means it is an undefined value as the root of a negative number is not a real number. For better understanding you can practice by yourself on the online compiler.

Must Read Decision Making in C

Frequently Asked Questions

What is the use of the pow() function?

pow() function returns the base to the exponent power, as in base^exponent , the base and the exponent are in a decimal numeral system.

Which header file is used for the pow() function?

In order to use pow(), we must include the math.h library in our application. The base value (x) and the exponent value are both sent to the pow() method.

How do I use the pow() function in C to calculate the power of a number?

To use the pow() function in C for calculating the power of a number, include the math.h header file, then call the pow() function with the base value and exponent as parameters. For example, pow(2, 3) calculates 2 raised to the power of 3, resulting in 8.

What happens when a negative base and a non-integer exponent are passed into the pow() function?

In general, pow(x,y) does not exist in the real numbers for the negative base and non-integer exponent. For positive x, however, pow(x,y) always exists and is real.

Conclusion

In this article, we took a look at the pow() function in C and also learned its operation. The pow() function in C offers a straightforward solution for calculating powers of numbers. By including the math.h header file, developers can leverage this function to efficiently compute exponentiation in their programs.

Recommended Articles