Write a program that computes the nth root of a number. Give a real number X gre
ID: 3646921 • Letter: W
Question
Write a program that computes the nth root of a number. Give a real number X greater than 1 and less than -1 and given an integer Y, compute the nth root Z such that Z*Z*Z*Z*Z*Z*Z..(Y times)..Z*Z=X, or Z^Y=X (Remember if X is negative, Y must be odd.) The user enters values for X and Y and the program calculates Z. You may only use simple arithmetic to do these computations. That is you may only use multiplication,division,addition and subtraction. Do it using 3 methods and implement it in 3 ways: one as static methods and one as an inheritance of the class.Explanation / Answer
public static double root(double num, double root) { if (num < 0) { return -Math.pow(Math.abs(num), (1 / root)); } return Math.pow(num, 1.0 / root); } public static void main(String[] args){ Scanner scn = new Scanner(System.in); double x = scn.nextDouble(); int y = scn.nextInt(); double result = root(x,y); System.out.println("result is :"+result); }Related 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.