JavaArithmetic Operators

Java Arithmetic Operators

Operators are one of the most important parts of any programming language. They allow us to perform different kinds of operations — from calculations to logic-based decisions.

In Java, operators are grouped into several types, such as:

  • Arithmetic Operators

  • Unary Operators

  • Assignment Operators

  • Relational Operators

  • Logical Operators

  • Ternary Operator

  • Bitwise Operators

  • Shift Operators

In this section, we’ll focus only on Arithmetic Operators, which are used for performing mathematical operations on data.

What Are Arithmetic Operators in Java?

Arithmetic operators are used to carry out basic math operations like addition, subtraction, multiplication, division, and finding the remainder. They can work on variables that store numeric values (like int, float, or double).

1. Addition (+)

The addition operator adds two numbers together.

Java
class Addition {
    public static void main(String[] args) {
        int num1 = 10, num2 = 20;
        int sum = num1 + num2;

        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        System.out.println("Sum = " + sum);
    }
}
num1 = 10
num2 = 20
Sum = 30
2. Subtraction (-)

This operator subtracts one number from another.

Java
class Subtraction {
    public static void main(String[] args) {
        int num1 = 20, num2 = 10;
        int sub = num1 - num2;

        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        System.out.println("Subtraction = " + sub);
    }
}
num1 = 20
num2 = 10
Subtraction = 10
3. Multiplication (*)

Used to multiply two numbers.

Java
class Multiplication {
    public static void main(String[] args) {
        int num1 = 20, num2 = 10;
        int product = num1 * num2;

        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        System.out.println("Multiplication = " + product);
    }
}
num1 = 20
num2 = 10
Multiplication = 200
4. Division (/)

This operator divides one number by another and returns the quotient.

Java
class Division {
    public static void main(String[] args) {
        int num1 = 20, num2 = 10;
        int result = num1 / num2;

        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        System.out.println("Division = " + result);
    }
}
num1 = 20
num2 = 10
Division = 2
5. Modulus (%)

The modulus operator returns the remainder when one number is divided by another.

Java
class Modulus {
    public static void main(String[] args) {
        int num1 = 5, num2 = 2;
        int remainder = num1 % num2;

        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        System.out.println("Remainder = " + remainder);
    }
}
num1 = 5
num2 = 2
Remainder = 1
Example: Using All Arithmetic Operators with User Input

Java
import java.util.Scanner;

public class ArithmeticOperators {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the first number: ");
        double num1 = sc.nextDouble();

        System.out.print("Enter the second number: ");
        double num2 = sc.nextDouble();

        double sum = num1 + num2;
        double difference = num1 - num2;
        double product = num1 * num2;
        double quotient = num1 / num2;

        System.out.println("The sum of the two numbers is: " + sum);
        System.out.println("The difference of the two numbers is: " + difference);
        System.out.println("The product of the two numbers is: " + product);
        System.out.println("The quotient of the two numbers is: " + quotient);
    }
}
Enter the first number: 20
Enter the second number: 10
The sum of the two numbers is: 30.0
The difference of the two numbers is: 10.0
The product of the two numbers is: 200.0
The quotient of the two numbers is: 2.0