Q.4 WAP to Given two integers x and n, compute 𝑥n.

Solution :- 

def power(base, exp): result = 1 for _ in range(abs(exp)): # Loop runs for absolute value of exponent result *= base if exp < 0: # If exponent is negative, take reciprocal return 1 / result return result x = int(input("Enter the base (x): ")) n = int(input("Enter the exponent (n): ")) result = power(x, n) print(f"{x} raised to the power {n} is: {result}")

Output :- 

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.