Write a program which (1) reads a double number from a user, and (2) stores the Math.sqrt of the number if it is > 0 and -1 otherwise into a variable named answer, and (3) return answer.
import java.util.Scanner; public class NumberSquareRoot { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); double number = in.nextDouble(); double answer; if (number > 0) { answer = Math.sqrt(number); } else { answer = -1; } System.out.println(answer); } }
Get Answers For Free
Most questions answered within 1 hours.