15,936,975 members
See more:
This question was asked in my interview -

I need to use compound assignments to change the program.

```class ArithmeticDemo {

public static void main (String[] args){

int result = 1 + 2; // result is now 3
System.out.println(result);

result = result - 1; // result is now 2
System.out.println(result);

result = result * 2; // result is now 4
System.out.println(result);

result = result / 2; // result is now 2
System.out.println(result);

result = result + 8; // result is now 10
result = result % 7; // result is now 3
System.out.println(result);

}
}```

What I have tried:

This is half the code I have tried in the interview -

```class ArithmeticDemo {

public static void main (String[] args){
int result = 3;```

I need help suggestions to complete this code. I tried to search on WEB but couldn't find anything on this and read some articles on Operators.

After intensive research found the answer on Oracle and on Scaler.
Posted
Updated 25-Apr-22 2:53am
v3

## Solution 1

You just need to apply all the operators in order, but use parentheses to counter the default precedence. See Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)[^].

## Solution 2

Definitely follow Richard's advice, and read up on operators - this is basic stuff so if you don't know it all pretty well, you aren't going to pass a verbal interview even if you hand in a perfect result for this task!

Then write each bit with parentheses round it:
```1 + 2
(1 + 2) - 1
((1 + 2) - 1) * 2
(((1 + 2) - 1) * 2) / 2
((((1 + 2) - 1) * 2) / 2) + 8
(((((1 + 2) - 1) * 2) / 2) + 8) % 7```
Then think about it: what parentheses can you remove?
For example: `a + b - c` is the same as `(a + b) - c` and `a + (b - c)`
But `a + b * c` will give you different result depending on which way you evaluate it: `(a + b) * c` is not the same as `a + (b * c)` so the parentheses are important.

Stop panicking, and think about what is required of you: this isn't a complex task so it shouldn't be difficult for you.