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


Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 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 Best Website and Software Company in Allahabad