Slide 1
Slide 2
Slide 3

Arithmetic Operators in java programming (V17)

No Comments

Arithmetic Operators

Arithmetic operators are used in arithmetic expressions in the same way they are used in mathematics.
Assume that variable A and B are integers, where A has a value of 10 and B has a value of 20. Then:

Operator Description Example
+ Adds values on either side of the operator. A + B = 30
- Subtracts the right-hand operand from the left-hand operand. B - A = 10
* Multiplies values on either side of the operator. A * B = 200
/ Divides the left-hand operand by the right-hand operand. B / A = 2
% Divides and returns the remainder. B % A = 0
++ Increases the value by 1. B++ = 21
-- Decreases the value by 1. B-- = 19

Source Code:

Copied!
public class ArithmeticOperators {
    public static void main(String[] args) {
        // Declare variables
        int a = 10, b = 5;

        // Arithmetic operations
        System.out.println("a + b = " + (a + b)); // Addition
        System.out.println("a - b = " + (a - b)); // Subtraction
        System.out.println("a * b = " + (a * b)); // Multiplication
        System.out.println("a / b = " + (a / b)); // Division
        System.out.println("a % b = " + (a % b)); // Modulus

        // Increment and Decrement
        System.out.println("++a = " + (++a)); // Pre-increment 11
        System.out.println("b++ = " + (b++)); // Post-increment 5
        System.out.println("b after post-increment = " + b);//6
        System.out.println("--a = " + (--a)); // Pre-decrement 10
        System.out.println("b-- = " + (b--)); // Post-decrement 6
        System.out.println("b after post-decrement = " + b); //5
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top