Java Operator Exercises
Exercises
In NetBeans, perform the following actions:
- Create three integer variables called num1, num2 and num3 – assign 10 and 20 to variables num1 and num2 respectively
- In one line of code: divide the sum of num1 and num2 by the difference of num2 and num1, then multiply the result by 3 and assign the final result to num3
- Output the value of num3 – this should be 9
- Increment num3 and multiple it by 10, assign the result to num3
- Output the value of num3 – this should be 100
- Find the remainder of num3 divided by 30
- Output the value of num3 – this should be 10
Click to reveal the solution
If you have finished or are REALLY stuck go ahead and click here to revel the solution...
int num1 = 10; int num2 = 20; int num3; num3 = ((num1 + num2) / (num2 - num1))*3; System.out.println(num3); num3 = ++num3 * 10; System.out.println(num3); num3 = num3%30; System.out.println(num3);
This will output:
run: 9 100 10 BUILD SUCCESSFUL (total time: 0 seconds)
What you have learnt
In these lessons you have learnt the basics of the most common operators in Java. The operators will enable your programs to perform common mathematics calculations, assign and compare values.