Write a program for each 1,2 &3 1)given an integer variable n and a double varia
ID: 3642041 • Letter: W
Question
Write a program for each 1,2 &31)given an integer variable n and a double variable x, write code to compute x^n. (Assume both has positive numbers stored in them)
2)Repeat 1 but compute n!(n factorial is n*(n-1)*(n-2)*....*3*2*1).
3) use the solution to #2 to compute e^x rising this formulate e^x= 1+ x+x^2/2!+x^3/3!.....+x^n/n!
Write a loop that tests your solution and output the results for various values of n.(the solution is exactly correct when n is infinite and approach the correct value as the value of n increases).
Explanation / Answer
class compute { public static void main(Strings[] a) { System.out.println(epowx(5)); } double static pow( double x, int n) { if(n==0)return 1; return x*pow(x,n-1); } double static fact( double n) { if(n==0|| n==1)return 1; return n*fact(n-1); } double static epowx(int n) { double sum =0; for(int i=0;iRelated 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.