In: Computer Science
UseMath
Write a program called UseMath. It should contain a class called UseMath that contains the method main. The program should ask for a number from the user and then print the information shown in the examples below. Look at the examples below and write your program so it produces the same input/output.
Examples
(the input from the user is in bold face) % java UseMath enter a number: 0 the square root of 0.0 is: 0.0 rounded to the nearest integer: 0 the value of PI is: 3.141592653589793 the value of PI plus your number is: 3.141592653589793 rounded to the nearest integer: 3 the value of E plus your number is: 2.718281828459045 the absolute value of your number is: 0.0 % java UseMath enter a number: 1 the square root of 1.0 is: 1.0 rounded to the nearest integer: 1 the value of PI is: 3.141592653589793 the value of PI plus your number is: 4.141592653589793 rounded to the nearest integer: 4 the value of E plus your number is: 3.718281828459045 the absolute value of your number is: 1.0 % java UseMath enter a number: -1 the square root of -1.0 is: NaN rounded to the nearest integer: 0 the value of PI is: 3.141592653589793 the value of PI plus your number is: 2.141592653589793 rounded to the nearest integer: 2 the value of E plus your number is: 1.718281828459045 the absolute value of your number is: 1.0 % java UseMath enter a number: .5 the square root of 0.5 is: 0.7071067811865476 rounded to the nearest integer: 1 the value of PI is: 3.141592653589793 the value of PI plus your number is: 3.641592653589793 rounded to the nearest integer: 4 the value of E plus your number is: 3.218281828459045 the absolute value of your number is: 0.5 % java UseMath enter a number: .1 the square root of 0.1 is: 0.31622776601683794 rounded to the nearest integer: 0 the value of PI is: 3.141592653589793 the value of PI plus your number is: 3.241592653589793 rounded to the nearest integer: 3 the value of E plus your number is: 2.818281828459045 the absolute value of your number is: 0.1 % java UseMath enter a number: 10 the square root of 10.0 is: 3.1622776601683795 rounded to the nearest integer: 3 the value of PI is: 3.141592653589793 the value of PI plus your number is: 13.141592653589793 rounded to the nearest integer: 13 the value of E plus your number is: 12.718281828459045 the absolute value of your number is: 10.0 %
if you have any doubts, please give me comment...
import java.util.Scanner;
public class UseMath{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("enter a number: ");
double num = scnr.nextDouble();
double sqrtNum = Math.sqrt(num);
System.out.println("the square root of "+num+" is: "+sqrtNum+" rounded to the nearest integer: "+Math.round(sqrtNum));
System.out.println("the value of PI is: "+Math.PI);
System.out.println("the value of PI plus your number is: "+(Math.PI+num)+" rounded to the nearest integer: "+Math.round(Math.PI+num));
System.out.println("the value of E plus your number is: "+(Math.E+num));
System.out.println("the absolute value of your number is: "+Math.abs(num));
}
}