Question

Write a Java Program Math.sqrt(). Calculate the square roots of 1, 2, 3, 4 to three...

Write a Java Program

Math.sqrt(). Calculate the square roots of 1, 2, 3, 4 to three decimal places. Print a column header " n sqrt(n)" and use the printf() method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Main {

        public static void main(String[] args) {

                System.out.printf("%-10s%-10s\n", "N", "SQRT(N)");
                
                for(int i=1; i<=4; i++) {

                        System.out.printf("%-10d%-10.2f\n", i, Math.sqrt(i));
                        
                }
                
        }

}

please upvote. Thanks!

Add a comment
Answer #2
public class SquareRootCalculator {
    public static void main(String[] args) {
        // Print column headers
        System.out.printf("%-5s %-8s%n", "n", "sqrt(n)");
        System.out.println("-----------");
        
        // Calculate and print square roots for numbers 1 through 4
        for (int n = 1; n <= 4; n++) {
            double sqrt = Math.sqrt(n);
            System.out.printf("%-5d %-8.3f%n", n, sqrt);
        }
    }
}




answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
Write a Java Program Math.sqrt(). Calculate the square roots of 1, 2, 3, 4 to three...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1.Write a Java program that computes the average of the values of a row in a...

    1.Write a Java program that computes the average of the values of a row in a twodimensional array. You should create a method with the following header: public static double averageRow(double[][] array, int row) Write a test program that reads a matrix from the user and a row index to calculate the average on. Print the result. 2.A square matrix is said to be an upper triangular matrix if the value of the cell is 0 when row > column....

  • Write a program in Java to find the roots of the equation x^3 - 18 =...

    Write a program in Java to find the roots of the equation x^3 - 18 = 0 Using the bisection method and Newton-Raphson Method. Compare the two solutions.

  • Please help! We use Java JGrasp. Write a Java program to take the input of 5...

    Please help! We use Java JGrasp. Write a Java program to take the input of 5 numbers and output the mean (average) and standard deviation of the numbers. If the numbers are x1, x2, x3, x4, and x5, the formula for mean is X = (x1 + x2 + x3 + x4 + x5)/5 And the formula for standard deviation is You can also break standard deviation down like this: 1. Work out the Mean (the simple average of the...

  • Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

    Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

  • In Java The square. In one program, write 2 separate functions, and use them. Prompt the...

    In Java The square. In one program, write 2 separate functions, and use them. Prompt the user for the length of one side of the square. Use it as input for the following 3 functions Test length of side sizes: 5, 42, 12.30 Calculate area of square, 1 input parameter sideLength. calcArea Calculate circumference of square, 1 input parameter sideLength: calcCircumference

  • Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array...

    Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array of 100 integers. Initialize the array to have all zeros. 2. Puts even numbers starting from 6 (excluding multiples of 12) into the first 50 slots of the array above. So numbers 6,8,10,14,16,18,20,22,26, etc. go into the first 50 positions in the array. Print the array. 3. Puts random numbers between 45 and 55 into the second 50 slots of the array above (second...

  • Write a java program that will read the values for 3 matrices A, B, and C...

    Write a java program that will read the values for 3 matrices A, B, and C and store the result of their summation in matrix D. You need to use a method to read matrix values. Use another method to add the three 3x3 matrices (each is a 2 dimensional array with 3 rows and 3 columns). Finally, use a third method to print the value of the summation (matrix D). Write a test program that will cal the three...

  • Write a Java program that prompts the user to enter a number representing a geometric shape....

    Write a Java program that prompts the user to enter a number representing a geometric shape. Options are 1 for square, 2 for rectangle, and 3 for right triangle. According to the user input, the program will collect either 1, 2 or 3 values from the user representing the lengths of the shape sides. The user is responsible for ensuring the 3 sides of the triangle represent a right triangle. The program will print the perimeter of the shape. Use...

  • Use printf(...) method to Write a java program that asks the user to enter three different...

    Use printf(...) method to Write a java program that asks the user to enter three different names and then sorts and display names in ascending order. For example, if the names "Maria", "Kelley", and "Angie" were entered, the program will display: Names in sorted order are: FIRST: Angie SECOND: Kelley THIRD: Maria

  • JAVA Write a method that returns the sum of all the elements in a specified column...

    JAVA Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public static double sumColumn(double [][] m, int columnIndex) Write another method that returns the sum of all the elements in a specified row in a matrix using the following header: public static double sumRow( double [][] m, int rowIndex) Write a test program that reads a 3-by-4 matrix and displays the sum of each column and sum...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT