Mathematical Library Methods
Which of the following is false to find square of a number?
- Math.pow(a,2)
- a*a
- Math.sqrt(a,2) ✓
- All of the above
What type of value is returned by Math.sqrt( )?
- int
- float
- double ✓
- All
Which of the following syntax is true to find the square root of a number?
- sqrt(a)
- Math.sqrt(a) ✓
- Squareroot(a)
- None
Name the class that is used for different Mathematical functions.
- Java.Math ✓
- Java.Power
- Java.Sqrt
- None
Give the output of the Math.abs(x); when x = -9.99
- -9.99
- 9.99 ✓
- 0.99
- None
Give the output of Math.sqrt(x); when x = 9.0
- 3
- 3.0 ✓
- 3.00
- all
System.out.println(Math.sqrt(10.24));
3.2
Math.sqrt method gives the square root of a positive number. Square root of 10.24 is 3.2 so it is the output.
System.out.println(Math.rint(-99.4));
-99.0
Math.rint method rounds off its argument to the nearest mathematical integer and returns its value as a double type. The nearest integer to -99.4 is -99.0 so that is the output. Math.rint method behaves in a particular way at the mid-point i.e. when the decimal part of the argument is 0.5. In such cases, the result is the integer value that is even. Let's understand this with an example. Math.rint(1.5) and Math.rint(2.5) will both return 2.0. In the case of 1.5, both 1.0 and 2.0 are equally close to 1.5. Math.rint choses the integer that is even so 2.0 is returned. In the case of 2.5, both 2.0 and 3.0 are equally close to 2.5. Math.rint again choses the integer that is even so 2.0 is returned.
System.out.println(Math.cbrt(42.875));
3.5
Math.cbrt method returns the cube root of its argument as a double value. Cube root of 42.875 is 3.5 so it is the output.
System.out.println(Math.min(-25.5, -12.5));
-25.5
Math.min method returns the smaller of its 2 arguments. As -25.5 is smaller than -12.5 so it is the output.
System.out.println(Math.ceil(-0.95));
-0.0
Math.ceil method returns the smallest double value that is greater than or equal to the argument and is equal to a mathematical integer. If the argument value is less than zero but greater than -1.0, then the result is negative zero which is the case in this question.
System.out.println(Math.round(-18.51));
-19
Math.round method rounds off its argument to the nearest mathematical integer and returns its value as an int or long type. At the mid-point i.e. when the decimal part of the argument is 0.5, Math.round method rounds up to the higher integer. In this case, the nearest integer to -18.51 is -19 so it is the output.
System.out.println(Math.max(-77.66, -87.45));
-77.66
Math.max method returns the greater of its 2 arguments. As -77.66 is greater than -87.45 so it is the output.
System.out.println(Math.floor(-0.88));
-1.0
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 -1.0 is the largest mathematical integer less than -0.88 so it is the output.
System.out.println(Math.rint(98.5));
98.0
Math.rint method rounds off its argument to the nearest mathematical integer and returns its value as a double type. This method behaves in a particular way at the mid-point i.e. when the decimal part of the argument is 0.5. In such cases, the result is the integer value that is even. Let's understand this with an example. Math.rint(97.5) and Math.rint(98.5) will both return 98.0. In the case of 97.5, both 97.0 and 98.0 are equally close to 97.5. Math.rint choses the integer that is even so 98.0 is returned. In the case of 98.5, both 98.0 and 99.0 are equally close to 98.5. Math.rint again choses the integer that is even so 98.0 is returned.
System.out.println(Math.ceil(65.5));
66.0
Math.ceil method returns the smallest double value that is greater than or equal to the argument and is equal to a mathematical integer. Here 66.0 is the smallest mathematical integer greater than 65.5 so it is the output.
To find the smaller between two numbers p and q
Answer
Math.min(p, q)
To find the absolute value of a number m
Answer
Math.abs(m)
To find the exponent of a number k
Answer
Math.exp(k)
To find the square root of a number d
Answer
Math.sqrt(d)
To find the rounded-off of a number b
Answer
Math.round(b)
Math.sqrt( );
Answer
double
Math.rint( );
Answer
double
Math.ceil( );
Answer
double
Math.round( );
Answer
int or long
Math.floor( );
Answer
double
Math.log( )
Answer
double
Math.random( )
Answer
Returns a positive double value, greater than or equal to 0.0 and less than 1.0.
Math.max( )
Answer
Returns the greater of its 2 arguments. Its return type is same as the type of its arguments.
Math.cbrt( )
Answer
Returns the cube root of its argument as a double value.
Math.abs( )
Answer
Returns the absolute value of its argument. Its return type is same as the type of its arguments.
Math.log( )
Answer
Returns the natural logarithm of its argument. Both return type and argument is of double data type.
Math.ceil( ) and Math.floor( )
Answer
Math.ceil( ) | Math.floor( ) |
---|---|
Returns the smallest double value that is greater than or equal to the argument and is equal to a mathematical integer | Returns 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. |
Math.rint( ) and Math.round( )
Answer
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 even | At 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 |
Write a program in Java to input three numbers and display the greatest and the smallest of the two numbers.
Hint: Use Math.min( ) and Math.max( )
Sample Input: 87, 65, 34
Sample Output: Greatest Number 87
Smallest number 34
import java.util.Scanner;
public class KboatGreatestNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter First Number: ");
int a = in.nextInt();
System.out.print("Enter Second Number: ");
int b = in.nextInt();
System.out.print("Enter Third Number: ");
int c = in.nextInt();
int g = Math.max(a, b);
g = Math.max(g, c);
int s = Math.min(a, b);
s = Math.min(s, c);
System.out.println("Greatest Number = " + g);
System.out.println("Smallest Number = " + s);
}
}
![BlueJ output of KboatGreatestNumber.java](https://cdn1.knowledgeboat.com/img/apc9/chap_6-p1.jpg)
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 = √p2 + b2
import java.util.Scanner;
public class KboatHypotenuse
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Perpendicular: ");
double p = in.nextDouble();
System.out.print("Enter Base: ");
double b = in.nextDouble();
double h = Math.sqrt(Math.pow(p, 2) + Math.pow(b, 2));
System.out.println("Hypotenuse = " + h);
}
}
![BlueJ output of KboatHypotenuse.java](https://cdn1.knowledgeboat.com/img/apc9/chap_6-p2.jpg)
Write a program to input a number and evaluate the results based on the number entered by the user:
(a) Natural logarithm of the number
(b) Absolute value of the number
(c) Square root of the number
(d) Cube of the number
(e) Random numbers between 0 (zero) and 1 (one).
import java.util.Scanner;
public class KboatMathMethods
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
double n = in.nextDouble();
System.out.println("Natural 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 = " + Math.random());
}
}
![BlueJ output of KboatMathMethods.java](https://cdn1.knowledgeboat.com/img/apc9/chap_6-p3.jpg)
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.
import java.util.Scanner;
public class KboatAvgMarks
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter Marks");
System.out.print("Physics: ");
int p = in.nextInt();
System.out.print("Chemistry: ");
int c = in.nextInt();
System.out.print("Biology: ");
int b = in.nextInt();
double avg = (p + c + b) / 3.0;
long roundAvg = Math.round(avg);
System.out.println("Rounded Off Avg Marks = " + roundAvg);
}
}
![BlueJ output of KboatAvgMarks.java](https://cdn1.knowledgeboat.com/img/apc9/chap_6-p4.jpg)
You want to calculate the radius of a circle by using the formula:
Area = (22/7) * r2; where r = radius of a circle
Hence the radius can be calculated as:
r = √((7 * area) / 22)
Write a program in Java to calculate and display the radius of a circle by taking area as an input.
import java.util.Scanner;
public class KboatCircleRadius
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Area of Circle: ");
double area = in.nextDouble();
double r = Math.sqrt(7 * area / 22);
System.out.print("Radius of Circle = " + r);
}
}
![BlueJ output of KboatCircleRadius.java](https://cdn1.knowledgeboat.com/img/apc9/chap_6-p5.jpg)