Given the following method: public double power(double x, int n) { System.out.pr
ID: 3608341 • Letter: G
Question
Given the following method: public double power(double x, int n) { System.out.println( "Inpower " + x + " : "+ n); if ( n ==0) return1.0; else if( n % 2 !=0) returnx * Math.pow(power(x, n/2), 2); else returnMath.pow(power(x,n/2), 2); } What is output by the callSystem.out.println(power(2.0,13));? Given the following method: public double power(double x, int n) { System.out.println( "Inpower " + x + " : "+ n); if ( n ==0) return1.0; else if( n % 2 !=0) returnx * Math.pow(power(x, n/2), 2); else returnMath.pow(power(x,n/2), 2); } What is output by the callSystem.out.println(power(2.0,13));?Explanation / Answer
Output will be : In power 2.0 : 13 In power 2.0 : 6 In power 2.0 : 3 In power 2.0 : 1 In power 2.0 : 0 8192.0 //here is trace of calledfunction. Calling power(2.0,13) Printing ... In power2.0 : 13 return x * Math.pow(power(x, n/2), 2) Calling power(2.0,6) Printing ...In power 2.0 : 6 return x *Math.pow(power(x, n/2), 2) Calling power(2.0,3) Printing ... In power 2.0 :3 return x * Math.pow(power(x, n/2), 2) Calling power(2.0,1) Printing ... In power 2.0 :1 return x * Math.pow(power(x, n/2), 2) Calling power(2.0,0) Printing ... In power 2.0 :0 return 1, n==0 8192.0 //print by System.out.print
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.