pow function in C : power of a number

pow function in C
pow function in C

Problem Statement: Find the Power of a number using the POW function.

Examples:

Example 1: 
Input: N = 5, k = 3
Output: 125
Explanation: 5*5*5 = 125

Example 2:
Input: N = 2, k = 3
Output: 8
Explanation: 2*2*2 = 8

pow function in C

DisclaimerDon’t jump directly to the solution, try it out yourself first.

Using library function pow(x,n), calculate x raised to the power n.

Definition: The pow() function takes two arguments (base value and power value) and, returns the value of base number raised to the given power value.

For example:

pow(2,3) will return value of 2^3 = 8

Function Signature:

double pow(double x, double y)

The function by default accepts values of type double and returns the result value as double.

If you need an integer result value, you can use type-casting to change the data type of the result.

Note: To type-case a variable to integer simply do: (int)variable_name

Code:

C Program

#include <stdio.h>
#include <math.h>
int main() {
	int x = 5;
	int n = 3;
	printf("%d raised to the power %d is %f",x,n,pow(5,3));
	return 0;
}

Output: 5 raised to the power 3 is 125.000000

Time Complexity: O(1)

Space Complexity: O(1)

Special thanks to Subhrajit Das for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article