Find out Power without Using POW Function in CThe function pow() is used to calculate the power of a given integer. Now in this article we will going to understand with a help of a program how to calculate the power of a integer without using the function pow() in C. Using for loop for Determining the Power of Given IntegerImagine you need to locate a ^ b. The easiest method is to multiply a by b times using a loop.
Let's understand the above approach better with an example of a program in C: Output: Enter Base: 5 Enter Power: 3 5 ^ 3 = 125 .......................... Process executed in 3.22 seconds Press any key to continue. Explanation The code above has an O (N) time complexity, where N is the exponent. O is the space complexity (1). Using While loop:Output: enter the number and its exponential : 5 4 5 ^ 6 = 625 .......................... Process executed in 0.11 seconds Press any key to continue. Explanation Long Long Int is twice as large as Long Int. The format specifier for long long int is percent lld. Using Recursion to find the Power of Given IntegerAssume that a ^ b is the input. The power of 'a' will increase by one with each recursive call. To obtain a ^ b, we call the recursive function b twice.
Let's understand the above approach better with an example of a program in C: Output: Enter Base: 5 Enter Power: 4 5 ^ 4 = 625 .......................... Process executed in 1.22 seconds Press any key to continue. Explanation: In the above example of a code in C, time complexity would be exponent N, O(N) & O(N) space complexity, internal stack. Next TopicExponential() in C |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India