2.14 LAB: Using math functions

Given three floating-point numbers x, y, and z, output the square root of x, the absolute value of (y minus z) , and the factorial of (the ceiling of z).


Output each floating-point value with two digits after the decimal point, which can be achieved as follows:

print('%0.2f %0.2f %0.2f' % (your_value1, your_value2, your_value3))


Ex: If the input is:


5.0

6.5

3.2

Then the output is:


2.24 3.30 24.00

Respuesta :

Answer:

In Java:

import java.util.Scanner;

import java.lang.Math;

public class MyClass {

   public static void main(String args[]) {

     Scanner input = new Scanner(System.in);

     float x, y, z;

     System.out.print("Enter three numbers: ");

     x = input.nextFloat();      y = input.nextFloat();      z = input.nextFloat();

     

     System.out.print("Square root of x: ");

     System.out.printf("%.2f",Math.sqrt(x));

     System.out.println();

     

     System.out.print("Absolute of y - z: ");

     System.out.printf("%.2f",Math.abs(y - z));

     System.out.println();

int fact = 1;

     for(int i = 1;i<=(int)Math.ceil(z);i++){

         fact*=i;

     }

     

     System.out.print("Factorial of z: "+fact);    

   }

}

Explanation:

This line declares x, y and z as float

     float x, y, z;

This line prompts user for three numbers

     System.out.print("Enter three numbers: ");

The next line get input for x, y and z

     x = input.nextFloat();      y = input.nextFloat();      z = input.nextFloat();

     

The italicized prints the square root of x rounded to 2 decimal places

     System.out.print("Square root of x: ");

     System.out.printf("%.2f",Math.sqrt(x));

     System.out.println();

The italicized prints the absolute of y - z to 2 decimal places      

     System.out.print("Absolute of y - z: ");

     System.out.printf("%.2f",Math.abs(y - z));

     System.out.println();

This initializes fact to 1

int fact = 1;

The following iteration calculates the factorial of ceil z

     for(int i = 1;i<=(int)Math.ceil(z);i++){

         fact*=i;

     }

     This prints the calculated factorial

     System.out.print("Factorial of z: "+fact);    

   }

}