# Java: Print the result of the specified operations

## Java Basic: Exercise-4 with Solution

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: **

**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