Power Function in C
0 192
The pow function in C is a part of the math.h library and is used to calculate the power of a given base raised to a specified exponent.
It takes two arguments: base and exponent
Syntax Power function in C:
double pow(double base, double exponent);
base: The base number.
exponent: The exponent to raise the base to.
Return Value:
The pow function returns the value of base raised to the power of exponent (base^exponent).
Example:
// Program for power function in C #include<stdio.h>#include<math.h> int main() { double base, exponent, result; printf("Enter the base number: "); scanf("%lf", &base); printf("Enter the exponent: "); scanf("%lf", &exponent); result = pow(base, exponent); printf("%.2lf raised to the power of %.2lf is %.2lf\n", base, exponent, result); return 0; }
Output:
Enter the base number: 4 Enter the exponent: 7 4.00 raised to the power of 7.00 is 16384.00
Usage of power function in C
The base and exponent can be any real numbers.
The exponent can also be a negative number, which would calculate the reciprocal of the base raised to the absolute value of the exponent.
Key Points:
Ensure to include the math.h header file before using the pow function.
Handle errors or invalid inputs, like raising a negative number to a non-integer exponent, which would result in a complex number (in real mathematics, not supported by C's pow function).
Share:
Comments
Waiting for your comments