CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 7 min · free

Mathematical Library Methods

7 min5 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Java's ready-made Math methods: sqrt, cbrt, pow, abs, max, min, round, ceil, floor and random, plus writing maths formulas as Java code.

Follows the syllabus of: CBSE Class 11 Information Technology (802), ICSE Class 9 Computer Applications, ICSE Class 10 Computer Applications, Cambridge IGCSE Grade 10 Computer Science (0478)

On screen in this lesson

The Math class

Math is a ready-made class in java.lang
No import needed
Call a method as Math.methodName(value)
Most methods return a double

Roots, powers and abs

MethodGivesExample
Math.sqrt(x)square rootsqrt(49) = 7.0
Math.cbrt(x)cube rootcbrt(27) = 3.0
Math.pow(a, b)a to power bpow(2, 10)=1024.0
Math.abs(x)value without signabs(-7) = 7

Rounding methods

MethodDoes4.5 gives
Math.round(x)nearest whole5
Math.ceil(x)next whole up5.0
Math.floor(x)next whole down4.0

Math.random()

Returns a double from 0.0 up to, not including, 1.0
Multiply to stretch the range
Cast with (int) to drop the decimal
Add the smallest value to shift the range

Writing formulas in Java

In mathsIn JavaNote
a² + b²a*a + b*bno ² symbol
√(a² + b²)Math.sqrt(a*a+b*b)* is needed
πr²Math.PI * r * rPI is a constant
(a + b) / 2(a + b) / 2.02.0 keeps decimal

Recap

sqrt, cbrt, pow return double; abs keeps the type
max and min compare exactly two values
round: nearest; ceil: up; floor: down
(int)(Math.random() * (max - min + 1)) + min
Every multiplication needs *, use Math.PI for pi

Quick answers

Why does Math.ceil(4.5) print 5.0 and not 5?

ceil returns a double, so it always shows a decimal part.

What does Math.round(-4.5) give?

-4, because a half always rounds up towards the bigger number.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Why does Math dot ceil of four point five print five point zero, and not just five? Today we learn Java's ready-made maths methods. We will find square roots, powers, the bigger of two numbers, rounding, and random numbers. Then we will turn a formula from your maths book into Java.

A library method is a method that Java has already written for us. Java keeps its maths methods in a class called Math, with a capital M, in the package java dot lang. This package is loaded automatically, so we do not need any import line. We call a method by writing Math, a dot, the method name, and the value in brackets. Most of these methods give back a double, which is a number with a decimal point.

Here are four common methods. Square root, written sqrt, gives the number which multiplied by itself makes x, so the square root of forty nine is seven point zero. Cube root, written cbrt, gives the number which multiplied three times makes x, so the cube root of twenty seven is three point zero. Pow of a and b gives a raised to the power b. So two to the power ten is one thousand twenty four point zero. Abs gives the absolute value, which is the number without its minus sign.

Let us run them. Square root of forty nine prints seven point zero, with a decimal, because sqrt always returns a double. Pow of two and ten prints one thousand twenty four point zero, again a double. Abs of minus seven prints seven, with no decimal. That is because abs returns the same type it receives, so an int gives an int.

Math dot max gives the bigger of two numbers, and Math dot min gives the smaller. Each takes exactly two values. So to find the topper among three students, we use max twice. First, max of Riya and Aman is seventy two. Then max of seventy two and Neha's sixty is still seventy two.

Pause and predict. Min of eight and three is easy, it prints three. Max of three and four point five gives four point five. Now the tricky one, max of five and five point zero. Both are equal, but one is a double, so Java treats both as doubles. The answer is five point zero, not five.

Java has three ways to round a number. Round gives the nearest whole number, and a half goes up, so four point five becomes five. Ceil, short for ceiling, always goes up to the next whole number, and returns a double. Floor always goes down to the whole number below, and also returns a double.

Board papers love negative numbers here. Think of a number line, where up means towards the right. Here x is minus four point five. Ceil goes up, to minus four point zero. Floor goes down, to minus five point zero. Round gives minus four, not minus five, because a half always rounds up towards the bigger number.

Math dot random gives a different number every time we run it. It returns a double that is at least zero, but always less than one. To get a bigger range, we multiply it. Then we cast it with int in brackets, which cuts off the decimal part. Finally we add the smallest value we want, to move the range up.

Let us roll a dice. Math dot random times six gives a number from zero up to just below six. Cast to int, and we get zero to five. Add one, and we get one to six. The general rule is random times max minus min plus one, cast to int, then add min. For a number from ten to twenty, we multiply by eleven, and add ten.

Now let us write maths formulas in Java. A squared plus b squared becomes a star a plus b star b, because Java has no small two symbol. For a square root, we put the whole expression inside Math dot sqrt, and every multiplication needs a star. Pi r squared uses Math dot pi, a constant already stored in the Math class. For an average, divide by two point zero, because int divided by int drops the decimal.

Here is a full formula. The radius r is seven. Area equals Math dot pi times r times r. The exact area is about one hundred fifty three point nine four. Math dot round gives the nearest whole number, so it prints one hundred fifty four.

Some exams ask for pseudocode instead of Java. Pseudocode is a plain, exam-style way to write steps, which is never run on a computer. The arrow means store the value. Round with two values rounds the area to zero decimal places. Random gives a number from zero to one, so this line gives a dice value from one to six.

Let us recap. Square root, cube root and power return a double, but abs keeps the type it gets. Max and min compare exactly two values. Round goes to the nearest whole number, ceil goes up, and floor goes down. For a random number in a range, use random times max minus min plus one, cast to int, plus min. And in a formula, every multiplication needs a star.

Courses that teach this

CourseUnit
CBSE Class 11 Information Technology (802)Part B, Unit 3: Office Automation Tools
ICSE Class 9 Computer ApplicationsMathematical Library Methods
ICSE Class 10 Computer ApplicationsRevision of Class IX Syllabus
Cambridge IGCSE Grade 10 Computer Science (0478)8. Programming

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →