0
0
Javaprogramming~10 mins

Arithmetic operators in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arithmetic operators
Start
Initialize variables
Apply +, -, *, /, %
Store results
Print results
End
This flow shows how arithmetic operators take values, perform calculations, and store or display the results.
Execution Sample
Java
int a = 10;
int b = 3;
int sum = a + b;
int diff = a - b;
int prod = a * b;
int quot = a / b;
int mod = a % b;
System.out.println(sum + ", " + diff + ", " + prod + ", " + quot + ", " + mod);
This code calculates sum, difference, product, quotient, and remainder of two numbers and prints them.
Execution Table
StepOperationExpressionResultExplanation
1Additionsum = a + b1310 + 3 = 13
2Subtractiondiff = a - b710 - 3 = 7
3Multiplicationprod = a * b3010 * 3 = 30
4Divisionquot = a / b3Integer division 10 / 3 = 3
5Modulomod = a % b1Remainder of 10 / 3 is 1
6PrintSystem.out.println(...)13, 7, 30, 3, 1Outputs all results
7End--Program ends
💡 All arithmetic operations done and results printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
a10101010101010
b3333333
sumundefined131313131313
diffundefinedundefined77777
produndefinedundefinedundefined30303030
quotundefinedundefinedundefinedundefined333
modundefinedundefinedundefinedundefinedundefined11
Key Moments - 3 Insights
Why does division 10 / 3 give 3 instead of 3.333?
Because both a and b are integers, Java does integer division which discards the decimal part. See execution_table step 4.
What does the % operator do?
It gives the remainder after division. For 10 % 3, the remainder is 1 as shown in execution_table step 5.
Are the original variables a and b changed by the operations?
No, a and b keep their original values throughout. The results are stored in new variables like sum, diff, etc. See variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of prod?
A7
B13
C30
D3
💡 Hint
Check the 'Result' column at step 3 in execution_table.
At which step does the modulo operation happen?
AStep 5
BStep 2
CStep 4
DStep 1
💡 Hint
Look for '%' operator in the 'Operation' column in execution_table.
If variable b was 4 instead of 3, what would be the new value of quot at step 4?
A4
B2
C3
D1
💡 Hint
Integer division 10 / 4 equals 2, check how quot is calculated in execution_table step 4.
Concept Snapshot
Arithmetic operators in Java:
+ (add), - (subtract), * (multiply), / (divide), % (modulo)
Operate on numbers and produce results.
Integer division discards decimals.
Modulo gives remainder.
Store results in variables.
Full Transcript
This example shows how Java uses arithmetic operators to calculate sum, difference, product, quotient, and remainder of two integers. Variables a and b start with 10 and 3. Each operator is applied step-by-step: addition gives 13, subtraction 7, multiplication 30, division 3 (integer division), and modulo 1 (remainder). Results are stored in separate variables. The program prints all results. Important points: integer division truncates decimals, modulo returns remainder, and original variables remain unchanged.