Mathematical Library Methods

I. Choose the correct answer:

  1. Which of the following is false to find 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
  6. Give the output of Math.sqrt(x); when x = 9.0.
    a) 3
    b) 3.0
    c) 3.00
    d) All

II. Predict the output:

  1. System.out.println(Math.sqrt(10.24));
    3.2
  2. System.out.println(Math.rint(-99.4));
    -99.0
  3. System.out.println(Math.cbrt(42.875));
    3.5
  4. System.out.println(Math.min(-25.5, -12.5));
    -25.5
  5. System.out.println(Math.ceil(-0.95));
    -0.0
  6. System.out.println(Math.round(-18.51));
    -19
  7. System.out.println(Math.max(-77.6, -87.45));
    -77.6
  8. System.out.println(Math.floor(-0.88));
    -1.0
  9. System.out.println(Math.rint(98.5));
    98.0
  10. System.out.println(Math.ceil(65.5));
    66.0

III. Write down the syntax for the following functions:

  1. To find the smaller between two numbers p and q.
    Math.min(p, q)
  2. To find the absolute value of a number m.
    Math.abs(m)
  3. To find the exponent of a number k.
    Math.exp(k)
  4. To find the square root of a number d.
    Math.sqrt(d)
  5. To find the rounded-off value of a number b.
    Math.round(b)

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

  1. Math.sqrt() – double
  2. Math.rint() – double
  3. Math.ceil() – double
  4. Math.round() – int or long
  5. Math.floor() – double
  6. Math.log() – double

V. Explain the following functions:

  1. Math.random() – It returns a random number from 0 to less than 1. It always returns a value of type double.
  2. Math.max() – It returns the maximum of the given two arguments. Its return type depends on the arguments passed.
  3. Math.cbrt() – It returns the cube root of a given positive or negative number. It always returns a value of type double.
  4. Math.abs() – It returns the absolute value of a given number. Its return type depends on the argument passed.
  5. Math.log() – It returns the natural logarithmic value of a given number. It always returns a value of type double.

VI. Distinguish between them using suitable examples:

  1. Math.ceil() and Math.floor()
    a) Math.ceil() returns the smallest integer of type double which is greater than or equal to the argument. Math.floor() returns the largest integer of type double that is less than or equal to the given argument.
    double a = Math.ceil(2.3); //a receives 3.0
    double b = Math.floor(2.3); //b receives 2.0
  2. Math.rint() and Math.round()
    Math.rint() returns the integer part in type double of a given number if the fractional part is up to 0.5, otherwise it returns the next higher integer value in type double. Math.round() returns the integer part in type double if the fractional part is less than 0.5, otherwise it returns the next higher integer value.
    double p = Math.rint(2.5); //p receives 2.0
    double q = Math.round(2.5); //q receives 3.0

VII. Unsolved Programs:

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

import java.io.*;
class Compare{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("First number: ");
        int a = Integer.parseInt(br.readLine());
        System.out.print("Second number: ");
        int b = Integer.parseInt(br.readLine());
        System.out.print("Third number: ");
        int c = Integer.parseInt(br.readLine());
        int big = Math.max(a, b);
        big = Math.max(big, c);
        int small = Math.min(a, b);
        small = Math.min(small, c);
        System.out.println("Greatest number: " + big);
        System.out.println("Smallest number: " + small);
    }
}

Q2. Write a program in Java to calculate and display the hypotenuse of a right-angled triangle by taking perpendicular and base as inputs.

import java.io.*;
class Triangle{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Perpendicular: ");
        double p = Double.parseDouble(br.readLine());
        System.out.print("Base: ");
        double b = Double.parseDouble(br.readLine());
        double h = Math.sqrt(p * p + b * b);
        System.out.println("Hypotenuse: " + h);
    }
}

Q3. 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 number between 0 and 1.

import java.io.*;
class Evaluate{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Number: ");
        double n = Double.parseDouble(br.readLine());
        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());
    }
}

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

import java.io.*;
class Exam{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Marks in Physics: ");
        double p = Double.parseDouble(br.readLine());
        System.out.print("Marks in Chemistry: ");
        double c = Double.parseDouble(br.readLine());
        System.out.print("Marks in Biology: ");
        double b = Double.parseDouble(br.readLine());
        double avg = (p + c + b) / 3.0;
        avg = Math.round(avg);
        System.out.println("Rounded average marks: " + avg);
    }
}

Q5. 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 input.

import java.io.*;
class Circle{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Area: ");
        double area = Double.parseDouble(br.readLine());
        double radius = Math.sqrt(7 * area / 22.0);
        System.out.println("Radius: " + radius);
    }
}

3 thoughts on “Mathematical Library Methods”

Leave a Reply

%d bloggers like this: