Exercise
Multiple operations and precedences
Objetive
Write a java program to print the result of the following operations:
-1 + 3 * 5
(24 + 5) % 7
15 + (-4) * 6 / 11
2 + 10 / 6 * 1 - 7 % 2
Example Code
// Main class definition
public class Program {
// Main method, entry point of the program
public static void main(String[] args) {
// Perform and store the result of the operation: -1 + 3 * 5
int result1 = -1 + 3 * 5;
// Perform and store the result of the operation: (24 + 5) % 7
int result2 = (24 + 5) % 7;
// Perform and store the result of the operation: 15 + (-4) * 6 / 11
int result3 = 15 + (-4) * 6 / 11;
// Perform and store the result of the operation: 2 + 10 / 6 * 1 - 7 % 2
int result4 = 2 + 10 / 6 * 1 - 7 % 2;
// Display each result on the screen
System.out.println("Result of -1 + 3 * 5 = " + result1);
System.out.println("Result of (24 + 5) % 7 = " + result2);
System.out.println("Result of 15 + (-4) * 6 / 11 = " + result3);
System.out.println("Result of 2 + 10 / 6 * 1 - 7 % 2 = " + result4);
}
}