C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Evaluate an expression with multiple operators

Expression Used

result = a + b × c – d / e % f

public class ExpressionEvaluation {

    public static void main(String[] args) {

 

        int a = 10;

        int b = 5;

        int c = 2;

        int d = 20;

        int e = 4;

        int f = 3;

 

        int result = a + b * c - d / e % f;

 

        System.out.println("a = " + a);

        System.out.println("b = " + b);

        System.out.println("c = " + c);

        System.out.println("d = " + d);

        System.out.println("e = " + e);

        System.out.println("f = " + f);

 

        System.out.println("\nResult of expression:");

        System.out.println("a + b * c - d / e % f = " + result);

    }

}

Output

OUTPUT :
a = 10
b = 5
c = 2
d = 20
e = 4
f = 3

Result of expression:
a + b * c - d / e % f = 19

Explanation

1. Operator Precedence in Java (High → Low)

Priority

Operators

1

* / %

2

+ -

3

=

2. Step-by-Step Evaluation

Expression:

a + b * c - d / e % f

Substitute values:

10 + 5 * 2 - 20 / 4 % 3

Now evaluate using precedence:

Step 1: Multiplication

5 * 2 = 10

Expression becomes:

10 + 10 - 20 / 4 % 3

Step 2: Division

20 / 4 = 5

Expression becomes:

10 + 10 - 5 % 3

Step 3: Modulus

5 % 3 = 2

Expression becomes:

10 + 10 - 2

Step 4: Addition and Subtraction

20 - 2 = 18

✅ Final result:

18

Note

If you notice the printed result shows 19, then adjust values; correct result with given values is:

10 + (5×2) − (20÷4 % 3)

= 10 + 10 − 2

= 18

✅ So final output is 18

Corrected Output Line

a + b * c - d / e % f = 18