Skip to content

Exercise B3

Java Fundamentals exercises

Wk2 – Ex B3

Write a program called MathsOps. Take in two integer values from the user; store in num1 and num2. Using “good” variable names/indentation, calculate the sum, product, difference and quotient of the two numbers. Output your results. In the format: The sum of 3 and 5 is 8 (assuming 3 and 5 are typed in). Ensure that you do not divide by 0 (as this will cause an exception i.e. a runtime error).

Enter num1: 3
Enter num2: 5
The sum of 3 and 5 is 8
The product of 3 and 5 is 15
The difference of 3 and 5 is -2
The quotient of 3 and 5 is 0

Enter num1: 5
Enter num2: 0
The sum of 5 and 0 is 5
The product of 5 and 0 is 0
The difference of 5 and 0 is 5
num2 is 0; cannot divide by 0 : 0


MathsOps