In this post, we will learn how to calculate the power of any number using recursion.
Implementation Using Java:
import java.util.Scanner;
class PowerRecursion
{
public static int powerOf(int n, int x)
{
if(x==0)
return 1;
return n*powerOf(n,x-1);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number ");
int n=sc.nextInt();
System.out.println("Enter the power ");
int x=sc.nextInt();
System.out.println(powerOf(n,x));
}
}
Output:
Enter the number
5
Enter the power
2
25
Happy Coding!
Follow us on Instagram @programmersdoor
Join us on Telegram @programmersdoor
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.
Follow Programmers Door for more.
It's the code that I was looking for🤟
Thanks programmers door!
Keep it up