top of page

Java Program to find Power using Recursion

navdeepajmani1

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.


15 views2 comments

Recent Posts

See All

2 Comments


Sudhanshu Mishra
Sudhanshu Mishra
Jun 06, 2020

It's the code that I was looking for🤟

Thanks programmers door!

Like

Prateek Chauhan
Prateek Chauhan
Jun 06, 2020

Keep it up

Like
bottom of page