Upload your JAVA code here in a .zip file. Be sure to upload your answer to the
ID: 3688840 • Letter: U
Question
Upload your JAVA code here in a .zip file. Be sure to upload your answer to the follow-up questions.
Lab Exercise 1 – Recursive Exponentiation
The problem is divided into six parts:
Lab Objectives
Description of the Problem
Sample Output
Program Template (Fig. L 8.1)
Follow-Up Questions and Activities
The program template represents a complete working C++ program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output; then study the template code.
Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the program. Compare your output with the sample output provided. Then answer the follow-up questions. The source code for the template is available from the Companion Website for C++ How to Program, Ninth Edition at www.pearsonhighered.com/deitel/.
Description of the Problem
Write a recursive function power(base, exponent) that, when invoked, returns
base exponent
For example, power(3,4) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint:
The recursive step would use the relationship
base exponent = base * base exponent -1
and the terminating (or base) case occurs when the exponent is equal to 1, because
base1 = base
Sample Output
Enter a base and an exponent: 3 4
3 raised to the 4 is 81
Template
// Lab 6
// Recursive exponentiation.
import java.util.Scanner;
public class RecursiveExponents {
public static void main(String [] args)
{
long b = 0;
long e = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a base and an exponent: ");
b = scan.nextLong();
e = scan.nextLong();
System.out.println(b + " raised to the " + e + " is " /*Write a call to function power using b and e as arguments*/);
}
//power recursively calculates base^exponent, assume exponent >= 1
/*Write the function header for the power function definition*/
{
if(/*Write the condition for the base case of the exponent equaling 1*/)
/*Write a return statement that returns the value of base*/
else//recursion step
/*Write a return statement that returns the value of base */
}//end function power
}
Fig. L 8.1 | RecursiveExponents.java.
Follow-Up Questions and Activities
Rewrite the power function to solve the problem iteratively (with a repetition structure) instead of recursively.
Explanation / Answer
Lab Objective Java program to find the base exponent.
Description of the Problem
Write a recursive function power(base, exponent) that, when invoked, returns base exponent.
Sample Output
Enter a base and an exponent: 3 4
3 raised to the 4 is 81
RecursiveExponents.java
package org.students;
import java.util.Scanner;
public class RecursiveExponents {
public static void main(String[] args) {
long b = 0;
long e = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a base and an exponent: ");
/* Getting the Inputs from the user. */
b = scan.nextLong();
e = scan.nextLong();
/* Displaying the result.*/
System.out.println(b + " raised to the " + e + " is "+exponent(b, e));
}
/* Method Which calculate the result when power rised to a base. */
public static long exponent(long b, long e)
{
if (e == 0) {
return 1;
} // base
else // recursion
{
b *= exponent(b, e - 1);
return b;
}
}
}
_________________________________________________________________________________________
output:
Enter a base and an exponent: 5 6
5 raised to the 6 is 15625
_________________________________
Enter a base and an exponent: 6 7
6 raised to the 7 is 279936
_________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.