TutorialsICSEClass 9Computer Applications

Math Library Methods in Java for ICSE Class 9 (with Outputs)

ICSE Class 9 Computer Applications notes on the Java Math class: sqrt, cbrt, pow, abs, max, min, ceil, floor, round, rint and random, with return types, tricky negative-number outputs and Java expressions for formulas.

10 min read
Math Library Methods in Java for ICSE Class 9 (with Outputs)

Questions on Math library methods appear in every ICSE Computer Applications paper, both as "give the output" and as "write the Java expression". They are easy marks, as long as you know each method's return type and how it treats negative numbers.

The Math class belongs to the java.lang package, which is imported automatically. Every method is called as Math.methodName(...).

The methods you must know

Math.sqrt(x) and Math.cbrt(x)

Square root and cube root. Both return a double.

Math.sqrt(16)   // 4.0
Math.sqrt(2)    // 1.4142135623730951
Math.cbrt(27)   // 3.0
Math.cbrt(-8)   // -2.0

Math.pow(a, b)

Raises a to the power b. It always returns a double.

Math.pow(2, 5)    // 32.0
Math.pow(4, 0.5)  // 2.0  (the same as a square root)
Math.pow(2, -1)   // 0.5

Math.abs(x)

The absolute value. The return type matches the argument: an int argument gives an int result, and a double gives a double.

Math.abs(-7)     // 7
Math.abs(-7.5)   // 7.5

Math.max(a, b) and Math.min(a, b)

The larger or smaller of two values. The return type also matches the arguments.

Math.max(4, 9)       // 9
Math.min(-2, -5)     // -5
Math.max(3, 4.5)     // 4.5  (int is promoted to double)

Math.ceil(x) and Math.floor(x)

  • ceil gives the smallest whole number greater than or equal to x.
  • floor gives the largest whole number less than or equal to x.

Both return a double.

Math.ceil(4.1)    // 5.0
Math.ceil(-4.1)   // -4.0   ← moves towards zero for negatives
Math.floor(4.9)   // 4.0
Math.floor(-4.1)  // -5.0   ← moves away from zero for negatives

Math.round(x)

Rounds to the nearest whole number. It works like floor(x + 0.5). With a double argument it returns a long, so the result has no decimal point.

Math.round(4.5)    // 5
Math.round(4.49)   // 4
Math.round(-4.5)   // -4   ← floor(-4.0)
Math.round(-4.6)   // -5

Math.rint(x)

Rounds to the nearest whole number but returns a double. When x is exactly halfway between two numbers, it chooses the even one.

Math.rint(2.5)   // 2.0
Math.rint(3.5)   // 4.0
Math.rint(2.7)   // 3.0

Math.random()

Returns a random double from 0.0 (inclusive) up to, but not including, 1.0.

int dice = (int) (Math.random() * 6) + 1;   // a random number from 1 to 6

Return types at a glance

  • double: sqrt, cbrt, pow, ceil, floor, rint, random
  • Same as the argument: abs, max, min
  • long (for a double argument): round
Writing 32 instead of 32.0 for Math.pow(2, 5) is the single most common mistake in output questions.

Writing Java expressions for formulas

  • √(b² − 4ac) → Math.sqrt(b * b - 4 * a * c)
  • (−b + √(b² − 4ac)) / 2a → (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a)
  • a³ + b³ → Math.pow(a, 3) + Math.pow(b, 3)
  • |x − y| → Math.abs(x - y)
  • ∛(x + y) → Math.cbrt(x + y)
Remember the brackets in / (2 * a). Writing / 2 * a divides by 2 and then multiplies by a.

Output questions

Q1. System.out.println(Math.ceil(Math.abs(-7.3)));

Answer: Math.abs(-7.3) gives 7.3, and Math.ceil(7.3) gives 8.0.

Q2. System.out.println(Math.max(Math.round(2.5), Math.floor(2.9)));

Answer: Math.round(2.5) gives 3 (a long) and Math.floor(2.9) gives 2.0 (a double). The max is promoted to double, so the output is 3.0.

Q3. double d = Math.pow(Math.sqrt(81), 2); System.out.println(d);

Answer: Math.sqrt(81) gives 9.0, and 9.0² is 81.0.

Solved program: roots of a quadratic equation

import java.util.Scanner;

class Quadratic {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a, b, c: ");
        double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble();
        double d = b * b - 4 * a * c;

        if (d < 0)
            System.out.println("Roots are not real");
        else {
            double r1 = (-b + Math.sqrt(d)) / (2 * a);
            double r2 = (-b - Math.sqrt(d)) / (2 * a);
            System.out.println("Roots: " + r1 + " and " + r2);
        }
    }
}

Sample run: for a = 1, b = −5, c = 6, the output is Roots: 3.0 and 2.0.

Practise more output questions in the ICSE Class 9 Computer Applications Sample Paper 2026-27.