Pages

Tuesday, 23 April 2013

Java code to calculate power of a number using recursion

Java program to calculate the power of a number using recursion.

import java.io.*;
class Power
{
 public static void main(String args[]) throws IOException
 {
 BufferedReader br = new BufferedReader(new
 InputStreamReader(System.in));
 Power call = new Power();
 System.out.print("Enter number : ");
 int x = Integer.parseInt(br.readLine());
 System.out.print("Enter power : ");
 int y = Integer.parseInt(br.readLine());
 System.out.println("\n" +x +"^" +y +" = "+call.findPower(x,y));
 }
 int findPower(int x, int y)
 {
 if(y==0)
 return 1;
 else if(y==1)
 return x;
 else
 return x*findPower(x,y-1);
 }
}

ALGORITHM:-

1. Start

2. Accept a number from user.

3. Ask user to enter the power.

4. Using method of recursion, find the value.

5. Print this value.

6. End

OUTPUT:-

Java code to calculate power of a number using recursion

Enter number : 4

Enter power : 3

4^3 = 64

Related Programs:-

Print the Factorial of a number using recursion

Print the possible combination of a string

Generate Fibonacci series using recursion

Find the HCF & LCM of two numbers using recursion

Find the sum of the following series using recursion : 12 + 22 + 32 +.......+ n2

1 comment:

  1. Great post. I want to advise you a great service to help in writing an essay http://essaylab.co.uk/.

    ReplyDelete

Back to Top