Find the Power Of Number In C
Introduction:
- The power of any given number can be determined using the power function. To use the power function, a predefined library function located in the math.h header file, we must import the header file called math.h file into our program.
- To put it another way, the pow() function raises the power of (y) to the power of (xy) to get the power of the base number (x).
- For instance, we can use the pow(2, 5) function to return the value 32 by taking the base as 2 and the power as 5, the power of a number.
- In order to do a similar thing, we use the pow(3, 4) function, passing 3 as the base and 4 as the exponent, to raise the base integer to a power of 3. This produces the result 81.
Syntax:
int pow (int a, int b);
Two integer arguments are provided to the pow() function to multiply the base value (a) by the exponent (b).
a: The base value, whose power is to be determined, is represented by the variable.
b: The power or exponent value is represented by the b variable.
A program that uses the pow() function to calculate the power of an integer number:
Let's look at an example of using the C programming language's pow() function to determine the power of an integer value.
#include <stdio.h>
#include <math.h>
int main ()
{
// Declare the variables base and exp.
int base, exp;
int res; // store the res
printf (" Enter the user's base value or input: ");
scanf (" %d", &base); // obtaining the user's base
printf (" Enter the power value for raising the power of base: ");
scanf (" %d", &exp); // obtain the user's exponent
// The base and the exp value can be passed as arguments using the pow() function.
res = pow ( base, exp);
printf (" %d to possess the power of %d is = %d ", base, exp, res);
return 0;
}
OUTPUT:
![Find the Power Of Number In C](https://static.tutorialandexample.com/c/find-the-power-of-number-in-c1.png)
A program that uses the pow() function to determine the power of a double data value:
Let's have a look at an illustration of the C programming language's pow() function using double data values.
#include <stdio.h>
#include <math.h>
int main ()
{
// Declare the variables base and exp.
double base, exp;
double res; // store the res
printf (" Enter the user's base value or input: ");
scanf (" %lf", &base); // obtaining the user's base
printf (" Enter the power value for raising the power of base: ");
scanf (" %lf", &exp); // obtain the user's exponent
// The base and the exp value can be passed as arguments using the pow() function.
res_num = pow ( base, exp);
printf (" %.1lf to possess the power of %.1lf is = %.2lf ", base, exp, res_num);
return 0;
}
OUTPUT:
![Find the Power Of Number In C](https://static.tutorialandexample.com/c/find-the-power-of-number-in-c2.png)
A program that uses a user-defined function to determine the power of a number:
Let's have a look at an example of how to write a user-defined function in the C programming language to determine a number's power.
#include <stdio.h>
#include <math.h>
int main ()
{
// The local variables are declared
long int base, exp;
printf (" Enter the user's base value or input: ");
scanf (" %d", &base); // obtaining the user's base
printf (" Enter the power value for raising the power of base: ");
scanf (" %d", &exp); // obtain the user's exponent
// The base and the exp value can be passed as arguments using the pow() function.
printf (" %d to possess the power of %d is = %d ", base, exp, power_number (base, exp));
}
// defining of a function or function call
int power_number (int a, int b)
{
int pow = 1, i; // declaration of the variables
for (i = 1; i <= b; ++i)
{
pow = pow * a;
}
return pow;
}
OUTPUT:
![Find the Power Of Number In C](https://static.tutorialandexample.com/c/find-the-power-of-number-in-c3.png)
A program that uses the recursion function and the pow() function to determine the power of a given number:
Let's look at an example where we use the C programming language's pow() function along with the recursion function to determine the power of a given number.
#include <stdio.h>
#include <math.h>
int main ()
{
// The local variables are declared
long int base, exp;
printf (" Enter the user's base value or input: ");
scanf (" %d", &base); // obtaining the user's base
printf (" Enter the power value for raising the power of base: ");
scanf (" %d", &exp); // obtain the user's exponent
// The base and the exp value can be passed as arguments using the pow() function.
printf (" %d to possess the power of %d is = %d ", base, exp, power_number (base, exp));
}
// defining of a function or function call
int power_number (int a, int b)
{
if ( b == 0)
{
return 1;
}
else
{
return power_number (a, b - 1) * a;
}
}
OUTPUT:
![Find the Power Of Number In C](https://static.tutorialandexample.com/c/find-the-power-of-number-in-c4.png)
Conclusion:
With the help of the pow() function, one can calculate the power of any type of numeric data (int, float, or double). The exponent number and base number are the two arguments for a power function. The exponent indicates the power of the base value, while the base value's base number denotes the power that needs to be determined.