Given base and exponent , find the power without using inbuilt functionInput FormatGiven two integers base and exponentOutput FormatPrint the powerConstraints1<= n <= 1000000NOTE : The calculated power value might exceed integer range.Sample Input 1:10 3Sample Output 1:1000Sample Input 2:3 4Sample Output 2:81
Question
Given base and exponent , find the power without using inbuilt functionInput FormatGiven two integers base and exponentOutput FormatPrint the powerConstraints1<= n <= 1000000NOTE : The calculated power value might exceed integer range.Sample Input 1:10 3Sample Output 1:1000Sample Input 2:3 4Sample Output 2:81
Solution
This problem can be solved using a simple iterative approach. Here are the steps:
- Initialize a variable, say
result, to 1. This will hold our final answer. - Run a loop from 1 to the exponent. In each iteration of the loop, multiply the
resultwith the base. - After the loop ends,
resultwill hold the final answer, i.e., base raised to the power exponent.
Here is a Python code snippet that implements the above steps:
def power(base, exponent):
result = 1
for i in range(exponent):
result *= base
return result
# Test the function
print(power(10, 3)) # Output: 1000
print(power(3, 4)) # Output: 81
This function takes two arguments, base and exponent, and returns the power of the base raised to the exponent. It uses a for loop to multiply the base to the result, exponent times. After the loop, it returns the result.
Please note that this function does not handle the case where the exponent is negative. If you need to handle negative exponents, you would need to modify the function to divide the result by the base for each iteration when the exponent is negative.
Similar Questions
Write a function to calculate power of a number raised to other.
Implement pow(x, n), which calculates x raised to the power n (i.e., xn). Example 1:Input: x = 2.00000, n = 10Output: 1024.00000Example 2:Input: x = 2.10000, n = 3Output: 9.26100
Implement pow(x, n), which calculates x raised to the power n (i.e., xn). Example 1:Input: x = 2.00000, n = 10Output: 1024.00000Example 2:Input: x = 2.10000, n = 3Output: 9.26100Example 3:Input: x = 2.00000, n = -2Output: 0.25000Explanation: 2-2 = 1/22 = 1/4 = 0.25 Constraints:-100.0 < x < 100.0-231 <= n <= 231-1n is an integer.Either x is not zero or n > 0.-104 <= xn <= 104
Find whether the given number is power of 2 or not.Input Format:Accept an Integer as inputOutput Format:Print the output as "YES" or "NO"Constraints:1<=INPUT<=10^15Sample Input 1:128Sample Output 1:YESSample Input 2:36Sample Output 2:NO
Given a non-negative integer N, the task is to check if N is a power of 2. Print 'YES' if N is the power of 2; else, print 'NO'.Example 1Input: N = 8Output: YESExplanation: 8 is equal to 2 raised to 3 (23 = 8).Example 2Input: N = 98Output: NOExplanation: 98 cannot be obtained by any power of 2.Note: This question was asked in Adobe coding test.Input format :The input consists of an integer N, representing the non-negative integer.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.