Question

Calculate power of a number using recursion

				
					ENTER THE NUMBER
10
ENTER THE POWER
10
10^10=10000000000
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class powerOfANumber
{
    long num=0,power=0;
    public void accept()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER");
        num=sc.nextLong();
        System.out.println("ENTER THE POWER");
        power=sc.nextLong();

    }

    public long calculatePower(long n,long pow)
    {
        if(pow==0)
        {
            return 1;
        }
        else
        {
            return n*calculatePower(n,pow-1);   
        }

    }
    public void display()
    {
        System.out.println(num+"^"+power+"="+calculatePower(num,power)); 
    }

    public static void main()
    {
        powerOfANumber ob1=new powerOfANumber();
        ob1.accept();
        ob1.display();

    }
}  



				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *