Introduction
The library function pow() is used to determine the power of any base; it is defined in the math.h header file.
We'll read X as the base and N as the power in this article, then calculate the result (XN - X to the power of N).
So, let’s get started:
Also Read, Binary to Hex Converter and C Static Function.
Problem Statement
Write a C program to input any two numbers x and n and find their power(xn) using the inbuilt pow() function.
About POW()
The pow() function calculates a number's power. The pow() function accepts two arguments (base and power) and returns the power raised to the base number.
In the math.h header file, the pow() function is defined.
Let's understand with an example:
Implementation
/* C program to find power of a number ( x^n ) using pow function */
#include <stdio.h>
#include <math.h>
void Solve()
{
int x,n;
int ans;
printf("Enter the value of base: \n");
scanf("%d",&x);
printf("\nEnter the value of power: \n");
scanf("%d",&n);
ans =pow((double)x,n);
printf("\n%d to the power of %d is= %d", x,n, ans);
}
int main()
{
Solve();
return 0;
}
Output:
Try it on the C compiler for a better understanding.
Program Explanation
The library function pow(), defined in the “math.h” header file, is used to compute mathematical functions in this C application. Using the 'x' and 'n' variables, we read two integer values and feed them to the power() function to compute XN.
Complexity Analysis
Time Complexity: O(log(N))
Space Complexity: O(1)
You can also read about dynamic array in c and Tribonacci Series.