Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Power Function in C

Last Updated on March 14, 2023 by Prepbytes

C programming language is widely used by programmers and in many cases, it is the first programming language for the programs that they learn. The C programming language provides many in-built functions and libraries for the programmers to use one such function in the power function. As the name suggests it is used to calculate the power of a number. We will learn more about the power function in c with its syntax, parameters, return values, and various examples of the same.

What is a Power Function?

The power function in c is represented by pow(). It is included in the math.h header file so we need to import the header file. The power function in c is a built-in function in the math library. It is used to calculate the power of a number. It will take two arguments the base and argument and will return the answer as the base number is raised to the power of the exponent.

Syntax of Power Function in C

The syntax of the power function in C is given below:

int pow(int base, int exponent);

Parameters of Power Function in C

The power function in c contains two parameters the base and the exponent.
We will calculate the value of the base and it will be the first argument in the power function in c. The exponent represents the power value the answer will be base to the power exponent.
In the above syntax both the base and exponent were int but there is no rule to take integers only we can also take the double values for the base and the exponent.

Return Value of the Power Function in C

The function will return according to the return type mentioned and the parameters declared inside it can either return an integer or double depending upon the factors explained above.

Working of Power Function in C

The power function takes two arguments the base and the exponent it will find the answer of the base raised to the exponential using a mathematical formula.

The formula that is used by the power function in c involves logarithms and exponential functions. The power function in c will calculate the logarithm of the base and then multiplies it by the exponent and after that will calculate the exponential function of the result. This will give the proper result.

The power function is also available in many other programming languages like python, c++, java, etc. The power function is widely used for mathematical valuations in various fields like scientific calculations, geometric calculations, and financial calculations. With this function, the programmer can perform complex operations quickly and efficiently.

Example 1 of Power Function in C: Both Parameters are Integer

Here we will discuss the code and implementation of the above-mentioned example.

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

int main()
{
    int x = 8; // base
    double y = 2;  // power

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

    return 0;
}

Output

64

Explanation of the above code
In the above example we have seen how to use the power function in c when both the input parameters are integers hence the return value is also an integer.

Example 2 of Power Function in C: When the Base is Integer and Exponent is Double

Now we will see the code implementation of the above-mentioned example.

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

int main()
{
    int a = 4; 
    double b = 2.3;  

    double ans = pow(a, b);
    
    // printing the output
    printf("%f", ans);

    return 0;
}

Output

24.251465

Explanation of the above example
In the above example we have seen that there is no need to keep both the parameters of same data type we have kept the base as integer whereas the exponent as double and because of that we get the output as double.

Example 3 of Power Function in C: Negative Values with Power Type of int

The code and implementation of the above example are given below.

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

int main() {
  printf("%f \n", pow(-7.5, -5));

  printf("%f", pow(7.5, -5));
  return 0;
}

Output

-0.000042 
0.000042

Explanation of the above example
In the above example we have seen the power of negative numbers with exponent type of integer and base as double and hence the return value is also double.

Example 4 of Power Function in C: Negative Values with Power Type of Double

We will now see the implementation of the above-mentioned example.

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

int main() {
  printf("%f \n", pow(-7.5, -5.5));

  printf("%f", pow(7.5, -5.5));
  return 0;
}

Output

-nan 
0.000015

Explanation of the above example
We can see in the above example that in one case it is giving the expected output and in another case, it is giving nan. This is because if the base is negative and the exponent is double the answer is double then we will get nan.

Example 5 of Power Function in C: Finding the Cube Root of a Number

The code and implementation of the above-mentioned example are given below.

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

int main()
{
double num = 27.0;
double result;

result = pow(num, 1.0/3.0);

printf("Cube root of %.1lf is %.1lf", num, result);

return 0;
}

Output

Cube root of 27.0 is 3.0

Explanation of the above example
In the above example we have used the power function in c to find the cube root of 27 we have just passed the exponent as 1.0/3.0.

Applications of Power Function in C

Some of the applications of power function are given below.

  • Scientific and engineering calculations: The power function is used in scientific and engineering calculations to find the values such as pressure, force, and energy.
  • Financial calculations: The power function is also used for calculating interest amounts, especially compound interest.
  • Statistical analysis: The IPower function is used to calculate the variance of a set of data.
  • Signal processing: In signal processing power function plays an important part as we need to find the square of the waves and then take samples and an average of the values.
  • Graphics and animation: The power function is also used in graphics and animation to create a realistic and smooth motion.

Conclusion
The power function is a very useful mathematical operation in C programming language. It is used to calculate the power of a number, square root, and cube root of a number. The pow() function takes two arguments, the base number, and the exponent. It returns the value of the base number raised to the power of the exponent.

In this article, we have discussed the power function in C language, its syntax, and how to use it with examples. We hope this article has helped you understand the power function in the C programming language.

Frequently Asked Questions

Below are some of the frequently asked questions about the power function in c.

1. Mention the range of the power function in C?
The power function in c varies from 0 to infinity.

2. Can we find the nth root of a number by using the power function?
Yes, we can find the nth root of a given number by just passing the exponent as 1.0/n.

3. What will be the output if the base of the power function in zero?
The answer will always be zero if the base in the power function is zero.

4. What will happen if the exponent of the power function is zero?
The answer will be one if the exponent of the power function is zero.

Leave a Reply

Your email address will not be published. Required fields are marked *