0% found this document useful (0 votes)
21 views2 pages

Source Code: Design A Class To Calculate The Value of M Using A Recursive Function

The document describes a Java program that uses a recursive function to calculate the value of m^n. The program prompts the user to input a number m and a power n, calls the recursive pow function to calculate the value, and displays the result. The pow function recursively multiplies the number m by itself b-1 times until the power b reaches 0, at which point it returns 1.

Uploaded by

Mohit K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Source Code: Design A Class To Calculate The Value of M Using A Recursive Function

The document describes a Java program that uses a recursive function to calculate the value of m^n. The program prompts the user to input a number m and a power n, calls the recursive pow function to calculate the value, and displays the result. The pow function recursively multiplies the number m by itself b-1 times until the power b reaches 0, at which point it returns 1.

Uploaded by

Mohit K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PROGRAM - 05

Design a class to calculate the value of mn 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

You might also like