Find power of a number

Ninja is sitting in an examination hall. He is encountered with a problem statement, "Find ‘X’ to the power ‘N’ (i.e. ‘X’ ^ ‘N’). Where ‘X’ and ‘N’ are two integers."

Ninja was not prepared for this question at all, as this question was unexpected in the exam.

He is asking for your help to solve this problem. Help Ninja to find the answer to the problem.

Note :

For this question, you can assume that 0 raised to the power of 0 is 1.

Input format :

The first line of input contains integer ‘T’ denoting the number of test cases. Then each test case follows:

The first and only line of each test case contains two integers ‘X’ and ‘N’ (separated by space).

Output Format :

Output will contain a single integer which will be equal to X ^ N (i.e. X raise to the power N).

Output of each test cases will be printed on separate line.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

Constraints:

1 <= T <= 5
0 <= X <= 10
0 <= N <= 10

Time Limit: 1 sec.
CodingNinjas
author
2y
Recursion

The approach is to recursively call the recursive function for X and N - 1 and multiplying the value returned by the recursive function with X.

As X ^ N is same as X * (X ^ (N - 1)).

Approach ...read more

CodingNinjas
author
2y
Optimized Approach

In this approach, we will call the recursive function for ‘X’ and ‘N / 2’. As X ^ N is same as

( X ^ (N / 2) ) * ( X ^ (N / 2) ) for N being an even number and X * ( X ^ (N / 2) ) *...read more

Help your peers!
Add answer anonymously...
Procol Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter