Source Code: Design A Class To Calculate The Value of M Using A Recursive Function
Source Code: Design A Class To Calculate The Value of M Using A Recursive Function
SOURCE CODE
import java.util.*;
public class Power
{
int power;
int res;
int m;
int n;
Power()
{
power = 0;
res = 0;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the number: ");
m = sc.nextInt();
System.out.println("Please enter the power: ");
n = sc.nextInt();
res = pow(m,n);
}
int pow(int a, int b)
{
if(b == 0)
{
return 1;
}
else
Page 1
{
return (a*pow(a,b-1));
}
}
void display()
{
System.out.println("Result = " + res);
}
public static void main()
{
Power ob = new Power();
ob.accept();
ob.display();
}
}
OUTPUT - 1
Please enter the number:
5
Please enter the power:
3
Result = 125
OUTPUT - 2
Please enter the number:
4
Please enter the power:
6
Result = 4096
Page 2