Use the API for the Math class javalangMath to answer the fo

Use the API for the Math class (java.lang.Math) to answer the following:
a. How can you use the Math class to calculate 2 to the 10th power?

b. How can you use the Math class to find the square roor of hundred?

c. How can you use the Math class to round a decimal number, 3.14150?

d. How can you use the Math class to generate a random number between 1 and 10, inclusive?

Solution

//Math.java
import java.lang.Math;

public class MathClass
{

public static void main(String[] args)
{
// Math class to calculate 2 to the 10th power
System.out.println(\"2 to the 10th power: \" + Math.pow(2,10));

// Math class to find the square roor of hundred
System.out.println(\"square root of 100: \" + Math.sqrt(100));

// Math class to round a decimal number, 3.14150
System.out.println(3.14150 + \" rounded to \" + Math.round(3.14150));   

System.out.println(\"random number betweeen 1 and 10: \" + (Math.random()*10+1));   

}

}

/*
output:

2 to the 10th power: 1024.0
square root of 100: 10.0
3.1415 rounded to 3
random number betweeen 1 and 10: 5.537877599561355

*/

Use the API for the Math class (java.lang.Math) to answer the following: a. How can you use the Math class to calculate 2 to the 10th power? b. How can you use

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site