Java Arithmetic Operators and Math Class Methods

A complete lesson with examples, assignments, and interactive MCQ questions implemented using JavaScript.

What you will learn

In this lesson, you will learn how Java performs basic mathematical calculations using arithmetic operators such as addition, subtraction, multiplication, division, and modulus. You will also learn how to use the Math class to perform common mathematical tasks such as finding the maximum value, square root, power, absolute value, and random number generation.

After reading the lesson, you can solve the assignments and then attempt the interactive quiz. The quiz checks your answers using JavaScript and gives instant feedback.

Part 1

Arithmetic operators in Java

Part 2

Math class methods

Part 3

Assignments for practice

Part 4

Interactive MCQ quiz

Arithmetic Operators in Java

Arithmetic operators are symbols used to perform mathematical operations on variables and values. These are some of the first operators every Java learner uses.

Operator Meaning Example Result
+ Addition 10 + 5 15
- Subtraction 10 - 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2
% Modulus or remainder 10 % 3 1

Example program

public class Main {
    public static void main(String[] args) {
        int a = 12;
        int b = 5;

        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
        System.out.println("Remainder: " + (a % b));
    }
}

Notice the division result. Since both a and b are integers, Java performs integer division. That means the decimal part is removed. So 12 / 5 becomes 2, not 2.4.

double x = 12;
double y = 5;
System.out.println(x / y); // 2.4

Increment and Decrement

Java also provides shortcut operators to increase or decrease a value by one.

int count = 7;

count++; // count becomes 8
count--; // count becomes 7 again

System.out.println(count);

The increment operator ++ adds one. The decrement operator -- subtracts one.

Math Class Methods in Java

The Math class in Java contains many useful built-in methods. These methods help you perform mathematical operations quickly without writing your own logic for every task.

Method Purpose Example Output
Math.max(a, b) Returns the larger value Math.max(7, 11) 11
Math.min(a, b) Returns the smaller value Math.min(7, 11) 7
Math.sqrt(x) Returns square root Math.sqrt(81) 9.0
Math.pow(a, b) Returns a raised to power b Math.pow(2, 4) 16.0
Math.abs(x) Returns absolute value Math.abs(-14) 14
Math.random() Returns random decimal from 0.0 to less than 1.0 Math.random() Example: 0.738...

Example program using Math methods

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.max(15, 22));
        System.out.println(Math.min(15, 22));
        System.out.println(Math.sqrt(64));
        System.out.println(Math.pow(3, 4));
        System.out.println(Math.abs(-90));
        System.out.println(Math.random());
    }
}

To generate a random integer from 1 to 100, you can write:

int randomNumber = (int)(Math.random() * 100) + 1;
System.out.println(randomNumber);

Assignments

Assignment 1: Write a Java program that takes two numbers and prints their addition, subtraction, multiplication, division, and remainder.
Assignment 2: Write a Java program to calculate the area and perimeter of a rectangle using arithmetic operators.
Assignment 3: Write a Java program to check whether a number is even or odd using the modulus operator.
Assignment 4: Write a Java program that finds the maximum and minimum of two given numbers using Math.max() and Math.min().
Assignment 5: Write a Java program to calculate the square root of a given number using Math.sqrt().
Assignment 6: Write a Java program to calculate a number raised to a power using Math.pow().
Assignment 7: Write a Java program to convert a negative number to a positive number using Math.abs().
Assignment 8: Write a Java program to generate a random number from 1 to 50.

Interactive MCQ Quiz

Select one answer for each question and click Check Answers. The quiz is implemented using JavaScript and shows your score instantly.

Total Questions: 0

0 / 0

Your result will appear here.

Quick Revision