Tutorialspoint
Problem
Solution
Submissions

Compute the Power of a Number

Certification: Basic Level Accuracy: 91.36% Submissions: 81 Points: 5

Write a Python program that calculates x raised to the power of y (x^y).

Example 1
  • Input: x = 2, y = 3
  • Output: 8
  • Explanation:
    • Step 1: Take base x = 2 and exponent y = 3 as input.
    • Step 2: Calculate 2^3 = 2 * 2 * 2 = 8.
    • Step 3: Return 8 as the result.
Example 2
  • Input: x = 5, y = -2
  • Output: 0.04
  • Explanation:
    • Step 1: Take base x = 5 and exponent y = -2 as input.
    • Step 2: Calculate 5^(-2) = 1/(5^2) = 1/25 = 0.04.
    • Step 3: Return 0.04 as the result.
Constraints
  • -10^6 ≤ x ≤ 10^6
  • -10^3 ≤ y ≤ 10^3
  • Time Complexity: O(log y) for manual implementation
  • Space Complexity: O(1)
NumberFunctions / MethodsRecursionGoogleTCS (Tata Consultancy Services)
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use the exponentiation operator: result = x ** y
  • Use math.pow(): import math; result = math.pow(x, y)
  • Implement binary exponentiation for more efficient manual calculation
  • Use recursion for manual implementation

The following are the steps to compute the power of a number:

  • Take two inputs: base and exponent.
  • Use the `**` operator or the `pow()` function.
  • Store the result.
  • Print the computed power.
  • Test with values like `2^3 = 8`.

Submitted Code :