This program prompts the user to input a base number and a power number, and calculates the result of raising the base to the power using a recursive function. Here's how the program works:
Overall, the program works correctly and efficiently to calculate the result of raising a number to a power using recursion.
import java.util.*; public class PowerNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.printf("Enter the Base Number : "); int base = input.nextInt(); System.out.printf("Enter the Power Number : "); int pow = input.nextInt(); int res = calulatePower(base, pow); System.out.printf("Result : " + res); } public static int calulatePower(int b, int p) { int res = 1; if (p > 0) { res = b * (calulatePower(b, p - 1)); } return res; } }
Enter the Base Number : 4 Enter the Power Number : 3 Result : 64
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions