Write the code to input a number and print the square root. Use the absolute value function to make sure that if the user enters a negative number, the program does not crash. Sample Run Enter a number: -16 Sample Output 4.0 Hint: Remember that you will need to import the math module at the beginning of your program.

Respuesta :

Answer:

Program in Java:

import java.lang.Math;

import java.util.Scanner;

public class MyClass {

   public static void main(String args[]) {

     Scanner input = new Scanner(System.in);

     double num;

     System.out.print("Enter any number: ");

     num = input.nextDouble();

     num = Math.abs(num);

     System.out.print("Square root: "+Math.sqrt(num));

   }

}

Explanation:

This line imports the math library

import java.lang.Math;

This line imports the scanner library

import java.util.Scanner;

public class MyClass {

   public static void main(String args[]) {

     Scanner input = new Scanner(System.in);

This line declares num as double

     double num;

This line prompts user for input of number

     System.out.print("Enter any number: ");

This line gets the input from the user

     num = input.nextDouble();

This line calculates the absolute value of the number input by the user

     num = Math.abs(num);

This line calculates and prints the square root of the number inputted

     System.out.print("Square root: "+Math.sqrt(num));

   }

}

ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE