Excursive java programming: We know that x^0 = 1 and x^n = x*x^(n-1). Write a re
ID: 674851 • Letter: E
Question
Excursive java programming:
We know that x^0 = 1 and x^n = x*x^(n-1). Write a recursive program with main string that calls to a program called exponentiation: This is how far I have gotten:
public class daemi2
{
public static double exponentiation (double x , double n)
{
if (n==0) return 1;
if (n==1) return x;
else
x=x*Math.pow(x,n-1);
return exponentiation(x ,n);
}
public static void main(String[]args)
{
double x = 4; // Base
double n = 4; // exponential
exponentiation(x , n );
System.out.print(exponentiation(x , n));
}
}
I can't seem to get exponantion to return x when it's in the right power and x= 256 = 4*4*4*4.
How should this look like?
Explanation / Answer
//changes are written in bold letters.
package daemi2;
public class daemi2
{
public static double exponentiation (double x , double n)
{
if (n==0) return 1;
// if (n==1) return x;
else
//x=x*Math.pow(x,n-1);
//return exponentiation(x ,n);
return (x*(exponentiation(x, n-1)));
}
public static void main(String[]args)
{
double x = 4; // Base
double n = 4; // exponential
// exponentiation(x , n, 1 );
System.out.print(x+"^"+n+" = "+exponentiation(x , n)+" ");
}
}
==================OUTPUT======================
run:
4.0^4.0 = 256.0
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.