Mathematical Library Methods ICSE Class 9: The Math Class in Java
Updated 15 October 2026 · Fact-checked
Mathematical library methods are ready-made methods of Java's Math class, in the default package java.lang, called as Math.methodName(arguments). ICSE Class 9 tests pow, sqrt, cbrt, ceil, floor, round, abs, max, min, random, rint, hypot, addExact, subtractExact and multiplyExact, their return types and outputs, and writing Java expressions with them.
What Mathematical Library Methods is about
Square roots, powers, rounding and random numbers are needed in many programs, from interest calculations to games. Java supplies them in the Math class so you do not have to write them yourself. Questions test exact outputs, so you must know what each method returns and in what type: Math.sqrt(64) prints 8.0, not 8, and Math.round(-4.5) is -4, not -5.
The CISCE syllabus asks for: an introduction to the package java.lang (the default package); the Math class methods pow(x, y), sqrt(x), cbrt(x), ceil(x), floor(x), round(x), abs(a), max(a, b), min(a, b), random(), rint(), hypot(a, b), addExact(int, int), subtractExact(int, int) and multiplyExact(int, int); and Java expressions using all the operators and Math methods.
Every output on this page was produced by running the code with OpenJDK 17.
Weightage: CISCE publishes no chapter weightage and no question-paper layout for ICSE Class 9 Computer Applications. Class IX is examined by your school: one 100-mark theory paper of 2 hours, plus 100 marks of internal assessment (at least 15 programming assignments and a written project on ethical computing). This is topic 7 of the 2028 syllabus.Source: CISCE ICSE 2028 Computer Applications syllabus, Class IX
Key concepts
- The Math class and java.lang
Math is a class in the package java.lang, which Java imports into every program automatically, so no import statement is needed.
Its methods are static: they are called using the class name, without creating an object: Math.sqrt(25), Math.pow(2, 3).
General form: Math.methodName(arguments)
- The arguments are the values given to the method.
- The method returns a value of a definite type, which can be printed, stored or used in an expression.
Key return types:
- double: pow, sqrt, cbrt, ceil, floor, rint, random, hypot (so their results print with .0 when whole).
- round: returns long for a double argument (int for a float argument).
- abs, max, min: return the same type as the arguments (after promotion).
- addExact, subtractExact, multiplyExact: return int for int arguments.
Worked example
(2 marks) Why is no import statement needed to use Math.sqrt()? Why is it called with the class name?
Try it first, then show the solutionSolution
- Math belongs to java.lang, which is imported by default - 1 mark.
- Its methods are static, so they are called as Math.method() without creating an object - 1 mark.
Answer: The Math class is in java.lang, the default package available to every program. Its methods are static, so they belong to the class and are called directly as Math.sqrt(x), without an object.
- pow, sqrt, cbrt and hypot
Math.pow(x, y): x raised to the power y; returns double.
Math.pow(2, 5) = 32.0; Math.pow(9, 0.5) = 3.0; Math.pow(2, -2) = 0.25.Math.sqrt(x): square root; returns double.
Math.sqrt(64) = 8.0; Math.sqrt(2) = 1.4142135623730951; Math.sqrt(-4) = NaN ('not a number', since a negative number has no real square root).Math.cbrt(x): cube root; returns double; works for negative numbers.
Math.cbrt(27) = 3.0; Math.cbrt(-64) = -4.0.Math.hypot(a, b): returns √(a² + b²), the hypotenuse of a right triangle with sides a and b; returns double.
Math.hypot(3, 4) = 5.0; Math.hypot(5, 12) = 13.0.
It gives the same result as Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)), which also gives 5.0 for 3 and 4.All outputs above were produced by running the code.
Worked example
(2 marks) Give the output: System.out.println(Math.pow(2, 5) + " " + Math.sqrt(64) + " " + Math.cbrt(-64) + " " + Math.hypot(5, 12));
Try it first, then show the solutionSolution
- pow(2, 5) = 32.0; sqrt(64) = 8.0 (double results) - 1 mark.
- cbrt(-64) = -4.0; hypot(5, 12) = 13.0 - 1 mark.
Answer: 32.0 8.0 -4.0 13.0
- ceil, floor, round and rint
Math.ceil(x): the smallest whole number greater than or equal to x (rounds up, towards +∞); returns double.
ceil(4.2) = 5.0; ceil(-4.2) = -4.0; ceil(7) = 7.0; ceil(-0.5) = -0.0.Math.floor(x): the largest whole number less than or equal to x (rounds down, towards −∞); returns double.
floor(4.8) = 4.0; floor(-4.2) = -5.0.Math.round(x): rounds to the nearest whole number; a half rounds up (towards +∞). It works like floor(x + 0.5). Returns long for a double (int for a float).
round(4.5) = 5; round(-4.5) = -4; round(-4.6) = -5; round(2.49) = 2.Math.rint(x): rounds to the nearest whole number, but when x is exactly halfway it chooses the even number; returns double.
rint(4.5) = 4.0; rint(5.5) = 6.0; rint(-2.5) = -2.0; rint(4.6) = 5.0.Rounding to 2 decimal places: Math.round(x * 100) / 100.0. For 2.567: round(256.7) = 257; 257 / 100.0 = 2.57.
Number-line rule: ceil moves right, floor moves left, round goes to the nearer (halves go right), rint goes to the nearer (halves go to the even neighbour).
Worked example
(2 marks) Give the output: System.out.println(Math.round(-4.5) + " " + Math.rint(4.5) + " " + Math.ceil(-4.2) + " " + Math.floor(-4.2));
Try it first, then show the solutionSolution
- round(-4.5): half goes towards +∞ = -4 (long, no .0); rint(4.5): halfway, even neighbour = 4.0 - 1 mark.
- ceil(-4.2) = -4.0; floor(-4.2) = -5.0 - 1 mark.
Answer: -4 4.0 -4.0 -5.0
- abs, max, min and random
Math.abs(a): the absolute (non-negative) value; same type as a.
abs(-7) = 7; abs(-7.5) = 7.5; abs(3) = 3.Math.max(a, b) and Math.min(a, b): the larger or smaller of two values; type follows the promoted arguments.
max(3, 8) = 8; min(-2, -9) = -9; max(4, 6.5) = 6.5 (int promoted to double); min('a', 'A') = 65 (chars promoted to int).
For three values nest them: Math.max(a, Math.max(b, c)).Math.random(): returns a random double from 0.0 (inclusive) up to but not including 1.0. No arguments.
To get a random whole number from 1 to n: (int) (Math.random() * n) + 1.
For a die: (int) (Math.random() * 6) + 1 gives 1 to 6. Because random() < 1, Math.random() * 6 is less than 6, so (int) gives 0 to 5; adding 1 gives 1 to 6.
General range from a to b: (int) (Math.random() * (b - a + 1)) + a.
(The output differs on each run, so questions ask for the range, not the exact value.)Worked example
(2 marks) Write an expression to generate a random integer from 10 to 20, both inclusive, and explain its range.
Try it first, then show the solutionSolution
- Count of values = 20 − 10 + 1 = 11: (int) (Math.random() * 11) + 10 - 1 mark.
- Math.random() * 11 is 0 to under 11; (int) gives 0 to 10; + 10 gives 10 to 20 - 1 mark.
Answer: (int) (Math.random() * 11) + 10. Math.random() * 11 lies from 0.0 to below 11.0, so casting gives 0 to 10, and adding 10 gives 10 to 20.
- addExact, subtractExact and multiplyExact
Math.addExact(a, b), Math.subtractExact(a, b) and Math.multiplyExact(a, b) return a + b, a − b and a × b for int arguments, exactly like the ordinary operators, except that if the true result does not fit in an int, they stop the program with an ArithmeticException (message: integer overflow) instead of silently giving a wrong answer.
As run in this study guide:
- Math.addExact(2000000000, 100000000) = 2100000000 (fits: the int maximum is 2,147,483,647).
- Math.subtractExact(50, 80) = -30.
- Math.multiplyExact(46340, 46340) = 2147395600 (just fits).
- Math.multiplyExact(100000, 100000) throws ArithmeticException: integer overflow.
- But 100000 * 100000 with the * operator prints 1410065408: a wrong value, with no warning, because the result overflowed.
Why use them: in billing, banking or counting programs, a silent overflow gives a believable but wrong number; the Exact methods make the error visible.
Worked example
(2 marks) What is the difference between 100000 * 100000 and Math.multiplyExact(100000, 100000) in Java?
Try it first, then show the solutionSolution
- The true product 10,000,000,000 exceeds the int range - 1 mark.
- * overflows silently and prints a wrong value (1410065408); multiplyExact throws ArithmeticException (integer overflow) - 1 mark.
Answer: Both multiply ints, but the product is too large for an int. The * operator overflows silently and gives the wrong value 1410065408, while Math.multiplyExact() stops the program with an ArithmeticException: integer overflow.
- Writing Java expressions with Math methods
Convert mathematical formulas into Java carefully:
- √(b² − 4ac) → Math.sqrt(b * b - 4 * a * c) or Math.sqrt(Math.pow(b, 2) - 4 * a * c)
- A = P(1 + r/100)ⁿ → a = p * Math.pow(1 + r / 100, n)
- |x − y| → Math.abs(x - y)
- ∛(a + b) → Math.cbrt(a + b)
- distance between (x1, y1) and (x2, y2) → Math.hypot(x2 - x1, y2 - y1) or Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
- the greatest of a, b, c → Math.max(a, Math.max(b, c))
Cautions:
- Use brackets for the whole argument: Math.sqrt(a + b), not Math.sqrt(a) + b.
- Watch integer division inside formulas: 1 / 2 is 0; use 0.5 or 1.0 / 2.
- Results of pow and sqrt are double; to store in int, cast: int c = (int) Math.pow(10, 3); gives 1000.
Compound interest (as run, P = 10000, r = 8, n = 3): a = 10000 × 1.08³ = 12597.12; Math.round(a) = 12597; interest rounded = 2597.
Worked example
(2 marks) Write Java expressions for: (i) v = √(u² + 2as); (ii) d = |a − b| ÷ ∛c
Try it first, then show the solutionSolution
- (i) v = Math.sqrt(u * u + 2 * a * s) - 1 mark.
- (ii) d = Math.abs(a - b) / Math.cbrt(c) - 1 mark.
Answer: (i) v = Math.sqrt(u * u + 2 * a * s);
(ii) d = Math.abs(a - b) / Math.cbrt(c);
Mathematical Library Methods in real life
Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Math.random() for games
Suppose you are writing a Ludo game app and need a die that shows 1 to 6 with equal chance.
Write the expression and explain why it never gives 0 or 7.
Try it first, then show the solutionSolution
- Math.random() gives 0.0 up to (not including) 1.0.
- Multiplied by 6: 0.0 up to below 6.0; (int) gives 0 to 5.
- Adding 1 gives 1 to 6.
Answer: (int) (Math.random() * 6) + 1. Since random() never reaches 1.0, the cast never gives 6, so after adding 1 the result is always from 1 to 6.
Memory hook: Multiply by how many, cast, add the start.
Rounding money
Imagine a shop's bill comes to Rs 12597.12 after a compound-interest instalment plan, and the shop charges to the nearest rupee.
Which method should be used, and what is charged?
Try it first, then show the solutionSolution
- Nearest whole number: Math.round().
- Math.round(12597.12) = 12597.
- Math.ceil would always round up, charging 12598.
Answer: Math.round(12597.12) = 12597, so Rs 12597 is charged. Math.ceil() would round any fraction up, to Rs 12598.
Memory hook: round for nearest, ceil for 'never less'.
hypot for distance
Suppose a delivery drone flies 3 km east and then 4 km north and you want the straight-line distance back to base.
Which Math method gives it directly?
Try it first, then show the solutionSolution
- The two legs form a right angle.
- Distance = √(3² + 4²).
- Math.hypot(3, 4) = 5.0.
Answer: Math.hypot(3, 4) returns 5.0, so the drone is 5 km from base.
Memory hook: hypot = hypotenuse.
Topics in this chapter
Mathematical Library Methods: syntax and key facts
- Powers and roots
- Math.pow(x, y) = xʸ; Math.sqrt(x) = √x; Math.cbrt(x) = ∛x; Math.hypot(a, b) = √(a² + b²)
- All return double; sqrt of a negative is NaN.
- Rounding
- 1.ceil
- 2.up (double); floor
- 3.down (double); round
- 4.nearest, half up (long/int); rint
- 5.nearest, half to even (double)
- round(-4.5) = -4; rint(4.5) = 4.0.
- Others
- abs(a) ≥ 0; max(a, b); min(a, b); random() in [0.0, 1.0)
- Random a..b: (int) (Math.random() * (b - a + 1)) + a
- Exact methods
- addExact, subtractExact, multiplyExact (int): ArithmeticException on overflow
- Ordinary operators overflow silently.
- Round to 2 d.p.
- Math.round(x * 100) / 100.0
- 2.567 → 2.57
How the school exam asks Mathematical Library Methods
The question patterns to expect, and the method that works for each.
| Question type | How it is asked | Method |
|---|---|---|
| MCQ and assertion-reason (1 mark) | Return type of a method; value of Math.ceil(-4.2); which method returns a random number. | Know the return types; use the number line for rounding. |
| Output (2 marks) | Print statements combining several Math methods. | Evaluate each call; remember .0 for double results and no .0 for round. |
| Java expressions (2 marks) | Write formulas such as √(b² − 4ac) or P(1 + r/100)ⁿ in Java. | Brackets around arguments; avoid integer division. |
| Program (15 marks) | Input values and compute using Math methods: triangles, interest, distances, rounding. | Scanner input, correct method calls, labelled output. |
Tricks to remember Mathematical Library Methods
Short memory aids. Say each one aloud once, then use it on a question the same day.
Ceiling up, floor down
The ceiling is above you (up); the floor is below you (down), even for negatives.
Use it when: ceil and floor.
round is long, the rest are double
pow, sqrt, cbrt, ceil, floor, rint, random, hypot print .0; round prints no .0.
Use it when: Output format.
rint likes even
At exactly .5, rint chooses the even neighbour: 4.5 → 4.0, 5.5 → 6.0.
Use it when: round vs rint.
Mathematical Library Methods: exam-level solved questions
Ordered from exam level to HOTS. Cover the solution, write your own, then compare step by step.
Example 1: Return type of sqrt · 1 mark · MCQExam level
What is printed by System.out.println(Math.sqrt(64));?
(a) 8
(b) 8.0
(c) 64.0
(d) 4.0
Try it first, then show the solutionSolution
- sqrt returns a double.
- √64 = 8, printed as 8.0.
Answer: (b) 8.0
Example 2: Assertion and reason: random · 1 mark · Assertion-reasonHarder exam level
Assertion (A): (int) (Math.random() * 6) + 1 can never give 7.
Reason (R): Math.random() returns a value greater than or equal to 0.0 and less than 1.0.
(a) Both A and R are true and R is the correct explanation of A
(b) Both A and R are true but R is not the correct explanation of A
(c) A is true but R is false
(d) A is false but R is true
Try it first, then show the solutionSolution
- random() < 1, so random() * 6 < 6 and the cast is at most 5.
- So the result is at most 6; R explains A.
Answer: (a) Both A and R are true and R is the correct explanation of A
Example 3: Rounding outputs · 2 marks · Short answerHarder exam level
Give the output:
System.out.println(Math.round(4.5) + " " + Math.round(-4.6) + " " + Math.round(2.49));
System.out.println(Math.rint(5.5) + " " + Math.rint(-2.5) + " " + Math.rint(4.6));
Try it first, then show the solutionSolution
- round: 4.5 → 5; −4.6 → −5; 2.49 → 2 (long, no decimal point).
- rint: 5.5 → 6.0 (even); −2.5 → −2.0 (even); 4.6 → 5.0.
Answer: 5 -5 2
6.0 -2.0 5.0
Example 4: Tricky ceil and floor · 2 marks · Short answerHOTS / competency
Give the output:
double x = Math.ceil(5.0001) - Math.floor(-5.0001);
System.out.println(x);
System.out.println(Math.max(Math.ceil(-0.5), Math.floor(0.5)));
Try it first, then show the solutionSolution
- ceil(5.0001) = 6.0; floor(-5.0001) = -6.0; 6.0 − (−6.0) = 12.0.
- ceil(-0.5) = -0.0 and floor(0.5) = 0.0; max gives 0.0.
Answer: 12.0
0.0
Example 5: max with mixed types · 2 marks · Very short answerHarder exam level
Give the output: System.out.println(Math.max(4, 6.5) + " " + Math.min('a', 'A') + " " + Math.abs(-7.5));
Try it first, then show the solutionSolution
- max(4, 6.5): 4 promoted to double: 6.5; min('a', 'A'): chars promoted to int: 65.
- abs(-7.5) = 7.5.
Answer: 6.5 65 7.5
Example 6: Write expressions · 2 marks · Very short answerExam level
Write Java statements for: (i) the greatest of three integers a, b, c; (ii) a random integer from 50 to 100; (iii) x rounded to two decimal places.
Try it first, then show the solutionSolution
- (i) int g = Math.max(a, Math.max(b, c)); (ii) int r = (int) (Math.random() * 51) + 50;
- (iii) double y = Math.round(x * 100) / 100.0;
Answer: (i) int g = Math.max(a, Math.max(b, c));
(ii) int r = (int) (Math.random() * 51) + 50;
(iii) double y = Math.round(x * 100) / 100.0;
Example 7: Right triangle program · 15 marks · Long answerHarder exam level
Write a program to accept the base and height of a right-angled triangle and print: the hypotenuse using Math.sqrt() and Math.pow(); the hypotenuse using Math.hypot(); the area; the perimeter rounded to two decimal places; and the longer of the two legs using Math.max().
Try it first, then show the solutionSolution
- import java.util.*;
class RightTriangle
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter base and height: ");
double b = sc.nextDouble();
double h = sc.nextDouble(); - double hyp1 = Math.sqrt(Math.pow(b, 2) + Math.pow(h, 2));
double hyp2 = Math.hypot(b, h);
double area = 0.5 * b * h;
double peri = b + h + hyp2; - System.out.println("Hypotenuse (sqrt, pow) = " + hyp1);
System.out.println("Hypotenuse (hypot) = " + hyp2);
System.out.println("Area = " + area);
System.out.println("Perimeter (2 d.p.) = " + Math.round(peri * 100) / 100.0);
System.out.println("Longer leg = " + Math.max(b, h));
}
} - Trace for 5 and 7: √(25 + 49) = √74 = 8.602325267042627; area = 0.5 × 5 × 7 = 17.5; perimeter = 20.602...; × 100 = 2060.2..., round = 2060, ÷ 100.0 = 20.6; max = 7.0.
Answer: Sample run (as run, input 5 7):
Enter base and height: 5 7
Hypotenuse (sqrt, pow) = 8.602325267042627
Hypotenuse (hypot) = 8.602325267042627
Area = 17.5
Perimeter (2 d.p.) = 20.6
Longer leg = 7.0
Case-study practice (Section B style)
Read the situation, then answer every part. In a school exam set on the board pattern this is a 7-mark question split into 2 + 3 + 2 marks.
A savings calculator
Imagine Nisha's bank app uses: double a = p * Math.pow(1 + r / 100, n); For p = 10000, r = 8 and n = 3 it shows Amount = 12597.12. The app must also show the amount to the nearest rupee and the interest to the nearest rupee, and a lucky-draw number from 1 to 100.
(i) · 2 marks
Write statements to print the amount and the interest, each rounded to the nearest rupee. What are the values?
Answer
Answer: System.out.println(Math.round(a)); System.out.println(Math.round(a - p)); They print 12597 and 2597.
1 mark statements, 1 mark values.
(ii) · 3 marks
Nisha's friend writes the formula as p * Math.pow(1 + r / 100, n) with r declared as int. For r = 8, what goes wrong and why?
Answer
Answer: With int r, r / 100 is integer division: 8 / 100 = 0, so the formula becomes p × 1ⁿ = p and no interest is added (Amount = 10000.0). Fix: declare r as double or write r / 100.0.
1 mark integer division, 1 mark result, 1 mark fix.
(iii) · 2 marks
Write an expression for the lucky-draw number and state its smallest and largest possible values.
Answer
Answer: (int) (Math.random() * 100) + 1; smallest 1, largest 100.
1 mark expression, 1 mark range.
Where the marks are
How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.
Give the output: Math.ceil(-4.2), Math.floor(-4.2), Math.round(-4.5), Math.rint(4.5). (2 marks) (2 marks)
| Step | Marks |
|---|---|
| -4.0 and -5.0 (ceil up, floor down, double). | 1 |
| -4 (round, half towards +∞, long) and 4.0 (rint to even, double). | 1 |
Program: right-angled triangle using sqrt, pow, hypot, round and max. (15 marks) (15 marks)
| Step | Marks |
|---|---|
| import, class, main(), Scanner input of base and height. | 3 |
| Hypotenuse with Math.sqrt(Math.pow(b, 2) + Math.pow(h, 2)). | 3 |
| Hypotenuse with Math.hypot(b, h); area. | 3 |
| Perimeter rounded with Math.round(peri * 100) / 100.0. | 3 |
| Math.max for the longer leg; labelled output. | 3 |
Where students lose marks in Mathematical Library Methods
The slips examiners see most often, and what to write instead.
Mistake: Writing 8 for Math.sqrt(64)
Thinking of the mathematical answer.
Do this: sqrt, pow, cbrt, ceil, floor, rint and hypot return double: 8.0.
Usually costs: 1 mark.
Mistake: Rounding negative halves the wrong way
Applying 'away from zero'.
Wrong working
Math.round(-4.5) = -5
Right working
Math.round(-4.5) = -4 (halves go towards +∞).
Do this: Think floor(x + 0.5): floor(-4.0) = -4.
Usually costs: 1 mark.
Mistake: Confusing ceil and floor for negatives
Thinking ceil makes the number 'bigger in size'.
Wrong working
Math.ceil(-4.2) = -5.0
Right working
Math.ceil(-4.2) = -4.0 (up the number line).
Do this: Draw a number line.
Usually costs: 1 mark.
Mistake: Random range off by one
Forgetting + 1 or the count.
Wrong working
(int) (Math.random() * 6) gives 0 to 5
Right working
(int) (Math.random() * 6) + 1 gives 1 to 6
Do this: (int) (Math.random() * count) + start, where count = end − start + 1.
Usually costs: 1 mark.
Practice questions on Mathematical Library Methods
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQExam level
Math.floor(-3.7) returns:
(a) -3.0
(b) -4.0
(c) -3
(d) -4Hint
Down the number line; double.
Answer
(b) -4.0
Q2 · 1 mark · MCQExam level
Which method returns the hypotenuse of a right triangle?
(a) Math.pow()
(b) Math.hypot()
(c) Math.cbrt()
(d) Math.max()Hint
Name says it.
Answer
(b) Math.hypot()
Q3 · 2 marks · Very short answerHarder exam level
State the return type of: (i) Math.round(5.6) (ii) Math.ceil(5.2) (iii) Math.abs(-5) (iv) Math.random()
Hint
round is special.
Answer
(i) long (ii) double (iii) int (iv) double
Q4 · 2 marks · Very short answerHarder exam level
Give the output: System.out.println(Math.sqrt(-4) + " " + Math.cbrt(27) + " " + Math.pow(2, -2));
Hint
No real root.
Answer
NaN 3.0 0.25
Q5 · 2 marks · Very short answerHarder exam level
Give the output: System.out.println((int) Math.pow(10, 3) + 1); System.out.println(Math.pow(4, 1.0 / 2) + Math.cbrt(8));
Hint
Cast then add; √4 + ∛8.
Answer
1001
4.0Q6 · 2 marks · Short answerHOTS / competency
Give the output: long lr = Math.round(7.8); int ir = Math.round(7.8f); System.out.println(lr + " " + ir); System.out.println(Math.abs(Math.round(-3.5)));
Hint
round(-3.5) goes towards +∞.
Answer
8 8
3 (Math.round(-3.5) = -3; abs gives 3)Q7 · 2 marks · Very short answerExam level
Write Java expressions: (i) root = (−b + √(b² − 4ac)) / 2a; (ii) the distance between points (x1, y1) and (x2, y2).
Hint
Brackets around 2 * a.
Answer
(i) root = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
(ii) d = Math.hypot(x2 - x1, y2 - y1); (or Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)))Q8 · 15 marks · Long answerHarder exam level
Write a program to accept principal, rate and years and print the amount with compound interest, and the amount and interest rounded to the nearest rupee.
Hint
Math.pow and Math.round.
Answer
import java.util.*;
class Interest
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Principal, rate (%), years: ");
double p = sc.nextDouble();
double r = sc.nextDouble();
int n = sc.nextInt();
double a = p * Math.pow(1 + r / 100, n);
double ci = a - p;
System.out.println("Amount = " + a);
System.out.println("Amount (nearest rupee) = " + Math.round(a));
System.out.println("Compound interest (nearest rupee) = " + Math.round(ci));
}
}
Sample run (as run, input 10000 8 3):
Amount = 12597.12
Amount (nearest rupee) = 12597
Compound interest (nearest rupee) = 2597Q9 · 3 marks · Short answerHOTS / competency
A billing program multiplies two int values with * and occasionally prints a negative total for very large orders. Explain the cause and how Math.multiplyExact() would help.
Hint
Overflow.
Answer
The product exceeds the int maximum (2,147,483,647), so it overflows and wraps round to a wrong, possibly negative, value without warning (100000 * 100000 gives 1410065408). Math.multiplyExact() would throw an ArithmeticException (integer overflow), so the error is noticed instead of a wrong bill being printed; using long for the total would also help.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.Package of the Math class?Show answer
java.lang (imported by default).
2.Math.cbrt(-64)?Show answer
-4.0
3.Range of Math.random()?Show answer
0.0 inclusive to 1.0 exclusive.
4.Math.rint(4.5)?Show answer
4.0
5.What does Math.addExact do on overflow?Show answer
Throws ArithmeticException (integer overflow).
6.Return type of Math.round(double)?Show answer
long
Best order to study it
- Learn java.lang and why Math methods are called with the class name.
- Learn pow, sqrt, cbrt and hypot with return types.
- Learn ceil, floor, round and rint on a number line, including negatives.
- Learn abs, max, min and random ranges.
- Learn the Exact methods; practise writing Java expressions and complete programs.
Quick revision checklist
The night before the exam: can you do each of these without looking?
- Math is in java.lang; static methods: Math.method(args).
- pow, sqrt (NaN for negatives), cbrt, hypot → double.
- ceil up, floor down (double); round nearest, half up (long); rint nearest, half to even (double).
- abs, max, min keep the promoted type; random() in [0.0, 1.0).
- addExact, subtractExact, multiplyExact: exception on int overflow.
- Random a..b: (int) (Math.random() * (b − a + 1)) + a; 2 d.p.: Math.round(x * 100) / 100.0.
Mathematical Library Methods: frequently asked questions
Why does Math.round() return long and not double?
Its result is always a whole number, so it returns an integer type: long for a double argument and int for a float argument. That is why it prints without .0.
What is NaN?
NaN means 'not a number'. It is the double result of an undefined operation such as Math.sqrt(-4) or 0.0 / 0.0.
Can Math methods be used inside other Math methods?
Yes. Calls can be nested, for example Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)) or Math.max(a, Math.max(b, c)).
Based on: CISCE ICSE 2028 Computer Applications (86) syllabus (Classes IX and X); CISCE Regulations for ICSE Examination Year 2028