Java: Print the result of the specified operations
Arithmetic Operations
Write a Java program to print the results of the following operations.
Pictorial Presentation:

Sample Solution-1
Java Code:
public class Exercise4 {
public static void main(String[] args) {
// Calculate and print the result of the expression: -5 + 8 * 6
System.out.println(-5 + 8 * 6);
// Calculate and print the result of the expression: (55 + 9) % 9
System.out.println((55 + 9) % 9);
// Calculate and print the result of the expression: 20 + -3 * 5 / 8
System.out.println(20 + -3 * 5 / 8);
// Calculate and print the result of the expression: 5 + 15 / 3 * 2 - 8 % 3
System.out.println(5 + 15 / 3 * 2 - 8 % 3);
}
}
Explanation:
The above Java code calculates and prints the results of four different arithmetic expressions:
- '-5 + 8 * 6' evaluates to '43'.
- '(55 + 9) % 9' evaluates to '1'.
- '20 + -3 * 5 / 8' evaluates to '19'.
- '5 + 15 / 3 * 2 - 8 % 3' evaluates to '13'.
Each expression is evaluated based on the order of operations (PEMDAS/BODMAS), which determines the sequence of arithmetic operations.
Note: PEMDAS stands for P- Parentheses, E- Exponents, M- Multiplication, D- Division, A- Addition, and S- Subtraction.
BODMAS stands for Bracket, Of, Division, Multiplication, Addition, and Subtraction.
Sample Output:
43 1 19 13
Flowchart:

Sample Solution-2
Java Code:
public class Main {
public static void main(String[] args) {
// Calculate and store the result of the expression: -5 + 8 * 6
int w = -5 + 8 * 6;
// Calculate and store the result of the expression: (55 + 9) % 9
int x = (55 + 9) % 9;
// Calculate and store the result of the expression: 20 + (-3 * 5 / 8)
int y = 20 + (-3 * 5 / 8);
// Calculate and store the result of the expression: 5 + 15 / 3 * 2 - 8 % 3
int z = 5 + 15 / 3 * 2 - 8 % 3;
// Print the calculated values, each on a new line
System.out.print(w + "\n" + x + "\n" + y + "\n" + z);
}
}
Flowchart:

For more Practice: Solve these Related Problems:
- Modify the program to take operations dynamically as user input.
- Write a program to evaluate a complex mathematical expression: (a^2 + b^2) / (a - b).
- Write a program that calculates a series sum: 1 + 1/2 + 1/3 + 1/4 + … + 1/n.
- Write a program that computes the sum of squares of two numbers.
Java Code Editor:
Previous: Write a Java program to divide two numbers and print on the screen.
Next: Write a Java program that takes two numbers as input and display the product of two numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics