« Write a program to find x to the power n (i.e. x^n)

Problem:

Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to return the answer. Do this recursively.

Input format :

Two integers x and n (separated by space)

Output format :

Print the output of x^n (i.e. x raise to the power n)

Constraints :

0 <= x <= 30 ; 0 <= n <= 30

Sample Input 1 :

3 4

Sample Output 1 :

81

Sample Input 2 :

2 5

Sample Output 2 :

32

Solution:

1#include<iostream>
2#include "Solution.h"
3using namespace std;
4
5int power(int x, int n) {
6 if(n == 0){
7 return 1;
8 }
9
10 return (x * power(x, n-1));
11}
12
13
14int main(){
15 int x, n;
16 cin >> x >> n;
17
18 cout << power(x, n) << endl;
19}