fork download
  1. import java.util.Scanner;
  2.  
  3. class MathTest
  4. {
  5. public static void main(String[] args)
  6. {
  7. // Create a scanner object for user input
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. // Prompt the user for an integer
  11. System.out.print("Enter an integer: ");
  12. int userInt = scanner.nextInt();
  13.  
  14. // Prompt the user for a double
  15. System.out.print("Enter a double: ");
  16. double userDouble = scanner.nextDouble();
  17.  
  18. // a. Calculate and display the square root of the integer
  19. double squareRoot = Math.sqrt(userInt);
  20. System.out.println("The square root of " + userInt + " is " + squareRoot);
  21.  
  22. // b. Generate and display a random number between 0 and the integer
  23. int randomValue = (int) (Math.random() * userInt);
  24. System.out.println("A random number between 0 and " + userInt + " is " + randomValue);
  25.  
  26. // c. Display the floor, ceiling, and rounded values of the double
  27. double floorValue = Math.floor(userDouble);
  28. double ceilingValue = Math.ceil(userDouble);
  29. long roundedValue = Math.round(userDouble);
  30.  
  31. System.out.println("The floor of " + userDouble + " is " + floorValue);
  32. System.out.println("The ceiling of " + userDouble + " is " + ceilingValue);
  33. System.out.println("The rounded value of " + userDouble + " is " + roundedValue);
  34.  
  35. // d. Display the larger and smaller values between the integer and the double
  36. double largerValue = Math.max(userInt, userDouble);
  37. double smallerValue = Math.min(userInt, userDouble);
  38.  
  39. System.out.println("The larger value between " + userInt + " and " + userDouble + " is " + largerValue);
  40. System.out.println("The smaller value between " + userInt + " and " + userDouble + " is " + smallerValue);
  41.  
  42. // Close the scanner
  43. scanner.close();
  44. }
  45. }
  46.  
Success #stdin #stdout 0.35s 61756KB
stdin
1 
2
stdout
Enter an integer: Enter a double: The square root of 1 is 1.0
A random number between 0 and 1 is 0
The floor of 2.0 is 2.0
The ceiling of 2.0 is 2.0
The rounded value of 2.0 is 2
The larger value between 1 and 2.0 is 2.0
The smaller value between 1 and 2.0 is 1.0