Computer Science, asked by harshnaveen7411, 1 year ago

Write a algorithm for power of x and y using function

Answers

Answered by lastbenchstudent
0

there are multiple ways to calculate

 {x}^{y}

1. using loop ( for or while )

2. using log

3. Using recursion

lets write suedo code for each one

1a) for loop

store x

store y

initialise result = 0 ( in type restricted language plz take it maximum range of number type if you dont know the boundaries)

for i = 1 to y

result = result * x

i = i +1

print ( result)

1a) using while loop

store x

store y

result = 0

while (y > 0){

result = result * x

y = y -1

}

print (result)

2. using log

x^y = inverse log ( log (x^y))= inverse log ( y*log x)

store x

store y

calculate result = y * log x using log function from math library

result = inverse log ( result )

print result

3. using recursion

power ( x , y) {

if y == 1 //base condition

return x

return x * power( x , y-1)

Similar questions