# Mathematical Library Methods

## State whether the following statements are 'True' or 'False'

#### Question 1

The return data type of Math.log() is double.
True

#### Question 2

Java.Math class is used for different mathematical functions.
True

#### Question 3

The output of Math.abs(-99.99) is 100.00.
False

#### Question 4

Math.sqrt(-225) can't be defined.
True

#### Question 5

The output of Math.cbrt(-3) will be -27.
False

#### Question 6

Math.round() returns the value in float data type.
False

## Predict the output

#### Question 1

`System.out.println(Math.floor(-4.7));`

##### Output
``````-5.0
``````
##### Explanation

Math.floor method returns the largest double value that is less than or equal to the argument and is equal to a mathematical integer. As -5.0 is the largest mathematical integer less than -4.7 so it is the output. Note that -4.7 is a negative number so the largest integer less than -4.7 is -5.0 and not -4.0.

#### Question 2

`System.out.println(Math.rint(-9.4)+Math.sqrt(9.0));`

##### Output
``````-6.0
``````
##### Explanation

Math.rint(-9.4) will give -9.0 as it is nearest integer to -9.4. Math.sqrt(9.0) will give 3.0. So, output will be -9.0 + 3.0 = -6.0

#### Question 3

`System.out.println(Math.max(Math.ceil(14.55),15.5));`

##### Output
``````15.5
``````
##### Explanation

Math.ceil(14.55) gives 15.0 as it is the smallest whole number greater than 14.55. After that Math.Max() becomes Math.max(15.0, 15.5). As 15.5 is greater than 15.0, it is the output.

#### Question 4

`System.out.println(Math.sqrt(Math.min(42.5,42.25)));`

##### Output
``````6.5
``````
##### Explanation

Math.min(42.5,42.25) gives 42.25. Math.sqrt(42.25) gives output as 6.5.

#### Question 5

`System.out.println(Math.ceil(3.4)+Math.pow(2,3));`

##### Output
``````12.0
``````
##### Explanation

Math.ceil(3.4) gives 4.0 and Math.pow(2,3) gives 8.0. So the output is 4.0 + 8.0 = 12.0.

## Write down the syntax for the following functions

#### Question 1

To find the natural log of q.

Math.log(q)

#### Question 2

To find the cube root of a number m.

Math.cbrt(m)

#### Question 3

To find the round-off of a number k.

Math.round(k)

#### Question 4

To find the absolute value of a number y.

Math.abs(y)

#### Question 5

To find the exponent of a number p.

Math.exp(p)

## Explain the following functions

#### Question 1

Math.random()

Returns a positive double value, greater than or equal to 0.0 and less than 1.0.

#### Question 2

Math.pow()

Returns the value of the first argument raised to the power of the second argument. Data type of the two arguments and the return type of this method is double.

#### Question 3

Math.cbrt()

Returns the cube root of its argument as a double value.

#### Question 4

Math.log()

Returns the natural logarithm of its argument. Both return type and argument is of double data type.

## Distinguish between

#### Question 1

Math.ceil() and Math.floor()

Math.ceil( )Math.floor( )
Returns the smallest double value that is greater than or equal to the argument and is equal to a mathematical integerReturns the largest double value that is less than or equal to the argument and is equal to a mathematical integer.
double a = Math.ceil(65.5);
In this example, a will be assigned the value of 66.0 as it is the smallest integer greater than 65.5.
double b = Math.floor(65.5);
In this example, b will be assigned the value of 65.0 as it is the largest integer smaller than 65.5.

#### Question 2

Math.rint() and Math.round()

Math.rint( )Math.round( )
Rounds off its argument to the nearest mathematical integer and returns its value as a double type.Rounds off its argument to the nearest mathematical integer and returns its value as an int or long type. If argument is float, return type is int, if argument is double, return type is long.
At mid-point, it returns the integer that is evenAt mid-point, it returns the higher integer.
double a = Math.rint(1.5);
double b =Math.rint(2.5);
Both, a and b will have a value of 2.0
long a = Math.round(1.5);
long b = Math.round(2.5);
a will have a value of 2 and b will have a value of 3

## Solutions to Unsolved Java Programs

#### Question 1

Write a program to calculate the value of the given expression:
1/a2 + 2/b2 + 3/c2
Take the values of a, b and c as input from the console. Finally, display the result of the expression to its nearest whole number.

```import java.util.Scanner;

public class KboatExpression
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the value of a: ");
int a = in.nextInt();

System.out.print("Enter the value of b: ");
int b = in.nextInt();

System.out.print("Enter the value of c: ");
int c = in.nextInt();

double exp = (1 / Math.pow(a, 2)) + (2 / Math.pow(b, 2)) + (3 / Math.pow(c, 2));
long roundedExp = Math.round(exp);

System.out.println("Result rounded to whole number: " + roundedExp);
}
}```
##### Output #### Question 2

For every natural number m>1; 2m, m2-1 and m2+1 form a Pythagorean triplet. Write a program to input the value of 'm' through console to display a 'Pythagorean Triplet'.
Sample Input: 3
Then 2m=6, m2-1=8 and m2+1=10
Thus 6, 8, 10 form a 'Pythagorean Triplet'.

```import java.util.Scanner;

public class KboatPythagoreanTriplet
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the value of m: ");
int m = in.nextInt();

if (m < 2) {
System.out.println("Invalid Input, m should be greater than 1");
return;
}

int firstTriplet = 2 * m;
int secondTriplet = (int)(Math.pow(m, 2) - 1);
int thirdTriplet = (int)(Math.pow(m, 2) + 1);

System.out.println("Pythagorean Triplets: "
+ firstTriplet
+ ", "
+ secondTriplet
+ ", "
+ thirdTriplet);

}
}```
##### Output #### Question 3

Write a program to input a number. Calculate its square root and cube root. Finally, display the result by rounding it off.
Sample Input: 5
Square root of 5= 2.2360679
Rounded form= 2
Cube root of 5 = 1.7099759
Rounded form= 2

```import java.util.Scanner;

public class KboatRoots
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the number: ");
int num = in.nextInt();

double sqrtValue = Math.sqrt(num);
double cbrtValue = Math.cbrt(num);
long roundSqrt = Math.round(sqrtValue);
long roundCbrt = Math.round(cbrtValue);

System.out.println("Square root of " + num + " = " + sqrtValue);
System.out.println("Rounded form = " + roundSqrt);
System.out.println("Cube root of " + num + " = " + cbrtValue);
System.out.println("Rounded form = " + roundCbrt);
}
}```
##### Output #### Question 4

The volume of a sphere is calculated by using formula:
v = (4/3)*(22/7)*r3
Write a program to calculate the radius of a sphere by taking its volume as an input.
Hint: radius = ∛(volume * (3/4) * (7/22))

```import java.util.Scanner;

public class KboatSphere
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter volume of sphere: ");
double v = in.nextDouble();

double r = Math.cbrt(v * (3 / 4.0) * (7 / 22.0));
System.out.println("Radius of sphere = " + r);
}
}```
##### Output #### Question 5

A trigonometrical expression is given as:
(Tan A - Tan B) / (1 + Tan A * Tan B)
Write a program to calculate the value of the given expression by taking the values of angles A and B (in degrees) as input.
Hint: radian= (22 / (7 * 180)) * degree

```import java.util.Scanner;

public class KboatTrigExp
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter angle A in degrees: ");

System.out.print("Enter angle B in degrees: ");
double angleBDeg = in.nextDouble();

double angleBRad = (22 * angleBDeg) / (7 * 180);

double trigExp = numerator / demoninator;

System.out.println("tan(A - B) = " + trigExp);
}
}```
##### Output #### Question 6

The standard form of quadratic equation is represented as:
ax2 + bx + c = 0
where d= b2 - 4ac, known as 'Discriminant' of the equation.
Write a program to input the values of a, b and c. Calculate the value of discriminant and display the output to the nearest whole number.

```import java.util.Scanner;

{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter a: ");
int a = in.nextInt();

System.out.print("Enter b: ");
int b = in.nextInt();

System.out.print("Enter c: ");
int c = in.nextInt();

double d = Math.pow(b, 2) - (4 * a * c);
long roundedD = Math.round(d);

System.out.println("Discriminant to the nearest whole number = " + roundedD);
}

}```
##### Output 