<p>This problem considers several ways to compute x^n for some<img src=\"https:/
ID: 3621607 • Letter: #
Question
<p>This problem considers several ways to compute x^n for some<img src="https://s3.amazonaws.com/answer-board-image/cramster-equation-20101111142886342508248829314436126.gif" alt="" align="absmiddle" /></p><p>a) write an iterative method power1 to comute x^n for   <img src="https://s3.amazonaws.com/answer-board-image/cramster-equation-20101111142886342508248829314436126.gif" alt="" align="absmiddle" /></p>
<p>b) write a recursive method power2 to compute x^n by using the following recursive formulation</p>
<p><img src="https://s3.amazonaws.com/answer-board-image/cramster-equation-201011111433446342508282496012743371.gif" alt="" align="absmiddle" />=1</p>
<p><img src="https://s3.amazonaws.com/answer-board-image/cramster-equation-201011111434586342508289855202055421.gif" alt="" align="absmiddle" /> if n>0</p>
Explanation / Answer
Dear, 1. Iterative x^n //Header file section import java.util.Scanner; //Begin class IterativePower class IterativePower { //recurtion public static int Power(int base, int exponent) { if ( exponent == 1 ) return base; else { int pow=base; for(int i=1;i 0 ) { int output = Power( base, exponent ); System.out.printf( "The Value is %d", output ); } else System.out.print( "Invalid Exponent " ); } //End main function } //End class 2.Recursive x^ n //Begin class RecursivePower class RecursivePower { //recurtion public static int Power(int base, int exponent) { if ( exponent == 1 ) return base; else return base * Power( base, exponent - 1 ); } //Begin main function public static void main( String args[] ) { //input Scanner Scanner scanner = new Scanner( System.in ); //For base number by the user System.out.print( "Enter base number : " ); //get base number int base = scanner.nextInt(); //For exponent number by user System.out.print( "Enter exponent number : " ); //get exponent number int exponent = scanner.nextInt(); if ( exponent > 0 ) { int output = Power( base, exponent ); System.out.printf( "The Value is %d", output ); } else System.out.print( "Invalid Exponent " ); } //End main function } //End class RecursivePowerRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.