Programming Examples

Java recursive function to find the power of number


Write a JAVA program to create a recursive function to calculate the power of number.

Solution:

import java.util.*;
class Power
{
    public static int pow(int b,int p)
    {
        if(p==1)     
        {
            return b;
        }
        else
        {
            
            return(b*pow(b,p-1));
        }
    }
    public static void main(String arr[]) 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the value of m and n ");
        int m=sc.nextInt();
        int n=sc.nextInt();
        int ans=pow(m,n);
        System.out.println(m+" power of "+n+" is : "+ans);
    }
}
Output

Enter the value of m and n 

5

3

5 power of 3 is : 125


CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz