# Chapter 6

Mathematical Library Methods

I. Choose the correct answer:

1. Which of the following is false to find the square of a number?

a) Math.pow(a,2)       b) a * a

c) Math.sqrt(a,2)        d) All of the above

2.What type of value is returned by Math.sqrt ( )?

a) int                                                                                   b) float

c) double                    d) All

3. Which of the following syntax is true to find the square root of a number?

a) sqrt(a)                     b) Math.sqrt(a)

c) Squareroot(a)         d) None

4. Name the class that is used for different Mathematical functions

a) Java.Math              b) Java.Power

c) Java.Sqrt                d) None

5. Give the output of the Math.abs(x) ; when x = – 9.99

a) -9.99                                                   b) 9.99

c) 0.99                                                                                d) None

Ans.

1. c) Math.sqrt(a, 2)

2. c) double

3. b) Math.sqrt(a)

4. a) java.math  (case sensitive)

5. b) 9.99

II. Predict the output:

1. System.out.println(Math.sqrt(10.24));                          Ans.      3.2

2. System.out.println(Math.rint(-99.4));                           Ans.   – 99.0

3. System.out.println(Math.cbrt(42.875));                        Ans.      3.5

4. System.out.println(Math.min(-25.5, -12.5));                 Ans.   – 25.5

5. System.out.println(Math.ceil(0.95));                             Ans.      1.0

6. System.out.println(Math.round(-18.51));                     Ans.   – 19

7. System.out.println(Math.max(-77.66, -87.45));             Ans.   – 77.66

8. System.out.println(Math.floor(-0.88));                         Ans.   – 1.0

9. System.out.println(Math.rint(98.5));                            Ans.      98.0

10. System.out.println(Math.ceil(65.5));                           Ans.      66.0

III. Write down the syntax of the following functions:

1. To find the smaller between two numbers p and q        Ans. Math.min(p,q)

2. To find the absolute value of a number m.                    Ans. Math.abs(m)

3. To find the exponent of a number k.                                                    Ans. Math.log(k)

4. To find the square root of a number d.                                                Ans. Math.sqrt(d)

5. To find the rounded-off of a number b.                                               Ans. Math.round(b)

IV. Predict the return data type of the following functions:

1. Math.sqrt( );                                       Ans. double

2. Math.rint( );                                       Ans. double

3. Math.ceil( );                                                                                             Ans. double

4. Math.round( );                                   Ans. integer

5. Math.floor( );                                     Ans. double

6. Math.log( );                                        Ans. double

V. Explain the following functions:

1. Math.random( )

Ans.

• This function returns a random number between 0 and 1. It returns a double data type value. Normally it returns the value as a fractional number. For example,   double d = Math. random( )
• To get an integer random number between 1 and n use as follows:

int r = (int) (Math.random( )*n ) + 1

• To get an integer random number between m and n where m and n are the lower and upper limits respectively, use as follows:

int r = (int) ((Math.random( )* (n – m)) +m);

2. Math. max( )

Ans.

This function is used to find the maximum of two given arguments. It returns a value depending upon the arguments (i.e. if the two arguments are integer, then the return type will be integer)

For example,

• int n = Math.max(23,38) ; This statement results in n=38, return type being int since arguments are int.
• double n = Math.max(34.25 , 12.7);    This statement results in n=34.5, return type being double since arguments are double.

3. Math.cbrt( )

Ans.

This function is used to find the cube root of a positive or negative number. It always returns a value in a double data type.

For example,

• double n = Math.cbrt(27.0) ; returns a double type value for n as 3.0
• double n = Math.cbrt(15.625);  returns a double type value for n as 2.5
• double n = Math.cbrt(– 3.375);  returns a value for n as – 1.5

4. Math.abs( )

Ans.

This function is used to return the absolute value of an argument. (i.e. magnitude of the number without its sign  i.e. a positive value). It returns int/ long/ double data type depending on the arguments supplied.

For example,

• int n = Math.abs(3);  returns an integer value for n as 3
• int n = Math.abs(– 8); returns an integer value for n as 8
• double d = Math.abs(– 9.99) returns a double value for n as 9.99

5. Math.log( )

Ans.

This function returns the natural logarithmic value of a given argument. It always returns a double type value.

For example,

double x = Math.log ( 6.25); returns a double type value for x as 1.8325

VI. Distinguish between them with suitable examples:

1. Math.ceil( ) and Math.floor( )

Ans.

Math.ceil( )

This function returns the smallest integer that is greater than or equal to the argument. It always returns a double data type whatever be the argument type ( int / float / double)

Example

i) For positive numbers

• System.out.println( Math.ceil(8));              output is 8.0
• System.out.println(Math.ceil(8.5));            output is 9.0
• System.out.println(Math.ceil(8.912));        output is 9.0

ii) For negative numbers

• System.out.println( Math.ceil( –  0.45 ));   output is  –  0.0
• System.out.println( Math.ceil( –  8));         output is  –  8.0
• System.out.println(Math.ceil(– 8.5));         output is –  8.0
• System.out.println(Math.ceil(– 8.912));     output is –  8.0

Math.floor( )

This function returns the largest integer that is less than or equal to the argument. It always returns the value as a double data type whether the argument is int/ float /double.

Example

i) For positive numbers

• System.out.println( Math.floor (8));           output is 8.0
• System.out.println(Math.floor (8.912));     output is 8.0

ii) For negative numbers

• System.out.println( Math.ceil( –  0.48 ));   output is  –  1.0
• System.out.println( Math.ceil( –  8));         output is  –  8.0
• System.out.println(Math.ceil(– 8.912));     output is  –  9.0

2. Math.rint( ) and Math.round( )

Ans.

Math.rint( )

It returns the truncated value of a number. It always returns an integer value in double data type. However, it predicts different values for positive and negative numbers.

For positive numbers

• If the fractional part lies between 0 and 0.5 (both inclusive) , then Math.rint( ) gives the integer part of the number.

For example ,

• Math.rint( 8.0)                     output is 8.0
• Math.rint(8.5)                      output is 8.0
• If the fractional part is more than 0.5, then Math.rint( )returns the next higher integer of the number.

For example,

• Math.rint(9.501)                  output is 10.0
• Math.rint(9.994)                  output is 10.0

For negative numbers

• If the fractional part lies between 0 (inclusive) and 0.5 (exclusive) , then Math.rint( ) gives the integer part of the number.

For example ,

• Math.rint( –9.0 )                   output is – 9.0
• Math.rint( –9.4)     output is – 9.0
• If the fractional part is 0.5 or  more than 0.5, then Math.rint( )returns the next lower integer of the number.

For example,

• Math.rint(–9.5)                    output is –10.0
• Math.rint( ­–9.9)                   output is –10.0

Math.round( )

This function returns the value in rounded-off form. If the fractional part is less than 0.5 then it returns the same value of the integer part of the argument. Otherwise, it returns the next higher integer value. This function always returns the integer value in long or int data type. However, it predicts different outputs for positive and negative numbers.

For positive numbers

• If the fractional part lies between 0 (inclusive) and 0.5 (exclusive) then the function , Math.round( ) returns the integer part of that number.

For example,

• Math.round(8.0)                   output is 8.
• Math.round(8.49)                 output is 8.
• If the fractional part is 0.5 or more then the function Math.round( ) returns the next higher integer.

For example,

• Math.round(8.5)                   output is 9.
• Math.round(8.99)                 output as 9.

For negative numbers

• If the fractional part lies between 0 and 0.5 (both inclusive) then the function , Math.round( ) returns the integer part of that number.

For example ,

• Math.round( –8.0)                output as –8.
• Math.round(–8.5)                 output as –8.
• If the fractional part is 0.5 or more then the function Math.round( ) returns the next higher integer.

For example ,

• Math.round( –8.51) output is –9.
• Math.round(–8.99)  output is –9.

VII. Unsolved Programs:

1. Write a program in Java to input three numbers and display the greatest and the smallest of two numbers.

Hint : Use Math.min( ) and Math.max( )

Sample Input : 87, 65, 34

Sample Output :

Greatest number 87

Smallest number 34

Solution:

import java.util.*;

class Q1

{

static void main()

{Scanner sr=new Scanner(System.in);

int a,b,c,big,small;

System.out.println(“Enter the numbers”);

a=sr.nextInt();

b=sr.nextInt();

c=sr.nextInt();

big=Math.max(c,Math.max(a,b));

small=Math.min(c,Math.min(a,b));

System.out.println(“The greatest of the 3 values is “+big);

System.out.println(“The smallest of the 3 values is “+small);

}}

• Write a program in Java to calculate and display the hypotenuse of a Right Angled Triangle by taking perpendicular and base as inputs.

Hint : h=

Solution:

import java.util.*;

class Q2

{

static void main()

{Scanner sr=new Scanner(System.in);

double p,b,h;

System.out.println(“Enter the perpendicular and then base”);

p=sr.nextDouble();

b=sr.nextDouble();

h= Math.sqrt(p*p + b*b);

System.out.println(“The value of hypotenuse =”+h);

}}

• Write a program to input a number and evaluate the results based on the number entered by the user:
• Natural logarithm of the number
•  Absolute value of the number
• Square root of the number
• Cube of the number
• Random numbers between 0 (zero) and 1 (one)                                           [ICSE 2006]

Solution:

import java.util.*;

class Q3

{

static void main()

{Scanner sr=new Scanner(System.in);

double n=0;

System.out.println(“Enter the number”);

n=sr.nextDouble();

System.out.println(“Logarithm=”+Math.log(n));

System.out.println(“Absolute value=”+Math.abs(n));

System.out.println(“Square root=”+Math.sqrt(n));

System.out.println(“Cube root=”+Math.cbrt(n));

System.out.println(“Random number between 0 and 1 is”+Math.random());

}}

• In an examination, you have appeared for three subjects i.e. Physics, Chemistry and Biology. Write a program in Java to calculate the average mark obtained and finally display the marks in rounded-off form.

Take Physics, Chemistry and Biology marks as inputs.

Solution:

import java.util.*;

class Q4

{

static void main()

{Scanner sr=new Scanner(System.in);

double phy,chem,bio,avg;

System.out.println(“Enter the marks”);

phy=sr.nextDouble();

chem=sr.nextDouble();

bio=sr.nextDouble();

avg=(phy+chem+bio)/3;

avg=Math.round(avg);

System.out.println(“Average =”+avg);

}}

• You want to calculate the radius of a circle by using the formula:

Area=(22/7)*r;  where r = radius of the circle.

Hence the radius can be calculated as:

r =

Write a program in Java to calculate and display the radius of a circle by taking area as an input.

Solution:

import java.util.*;

class Q5{

static void main()

{Scanner sn=new Scanner(System.in);

double area,r;

System.out.println(“Enter the area of the circle”);

area=sn.nextDouble();

r=Math.sqrt(7*area/22.0);

System.out.println(“The radius of the circle=”+r);

}}

*************