Question

IN JAVA In this problem, we will implement an nth root finder. Recall that the nth...

IN JAVA

In this problem, we will implement an nth root finder. Recall that the nth root of x is the number when raised to the power n gives x. In particular, please fill in the method findNthRoot(int number, int n, int precision) within the Main class. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should still fill in the answer with decimal places (i.e. with input 41 and precision 5, we should return 41.00000.) You may not use any library functions for this question.

For this question, our program expects 3 lines, where the first line is n (the degree of root to take), the second line is the actual number, and the third line represents the precision. The output is a single line containing the answer followed by a newline.

For example, the following input would generate the output 20.000:

 
 

2

 

400

 

3

Additionally, the following input would generate the output 6.86:

 
 

2

 

47

 

2

 

4

use this format for code

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
// Read n (for taking the nth root)
int n = Integer.parseInt(sc.nextLine());
  
// Read number to take the nth root of
int number = Integer.parseInt(sc.nextLine());
  
// Read the desired precision
int precision = Integer.parseInt(sc.nextLine());
  
// Print the answer
System.out.println(findNthRoot(number, n, precision));
}
  
private static String findNthRoot(int number, int n, int precision) {

}

Homework Answers

Answer #1

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Read n (for taking the nth root)
int n = Integer.parseInt(sc.nextLine());

// Read number to take the nth root of
int number = Integer.parseInt(sc.nextLine());

// Read the desired precision
int precision = Integer.parseInt(sc.nextLine());

// Print the answer
System.out.println(findNthRoot(number, n, precision));
}

private static String findNthRoot(int number, int n, int precision) {
    double x = number*1.0/n;
    double error = 1/.00000001;
    while (error > .0000001){//it will calculate for a precision of 6 decimalpoins
      
        x = ((n - 1.0) * x + number / Math.pow(x, n - 1.0)) / n;
        error = Math.abs(Math.pow(x,n)-number);
    }
    String str = "%."+precision+"f";
    return String.format(str,x);
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
I need to come up with the pseudocode for a min and max value finder for...
I need to come up with the pseudocode for a min and max value finder for an array, uses recursion. first I started writing a java program to get a better understanding but it seams not to work properly. also Im having trouble calculating the time efficiency for the code. public class Max_min {     /**      * @param args the command line arguments      */     static int Max;     static int Min;     static int index;     static...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
Write a java program that calculates the number of duplicate words in a sentence. You may...
Write a java program that calculates the number of duplicate words in a sentence. You may assume that the input consists only 52 English characters with a single white-space as the separator between words (not punctuations in the inputted sentence). You need consider uppercase and lowercase letters the same. You may get the input from the keyboard. (You need to use Set and/or Hashset). What I have so far import java.util.HashSet; import java.util.Scanner; public class Duplicate { int counter; int...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Complete the program below. The program takes in two positive integers, n and m, and should...
Complete the program below. The program takes in two positive integers, n and m, and should print out all the powers of n that are less than or equal to m, on seperate lines. Assume both n and m are positive with n less than or equal to m. For example, if the input values are n = 2 and m = 40 the output should be: 2 4 8 16 32 Starter code: import java.util.Scanner; public class Powers {...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT