w3resource
Java Programming Exercies

Java Exercises: Print the result of the specified operations

Java Basic: Exercise-4 with Solution

Write a Java program to print the result of the following operations.

Pictorial Presentation:

Java: Print the result of the specified operations

Sample Solution:

Java Code:

public class Exercise4 {
 
 public static void main(String[] args) {
  System.out.println(-5 + 8 * 6);
  System.out.println((55+9) % 9);
  System.out.println(20 + -3*5 / 8);
  System.out.println(5 + 15 / 3 * 2 - 8 % 3);
 }
 
}

Sample Output:

43                                                                                                      
1                                                                                                      
19                                                                                                      
13

Flowchart:

Flowchart: Java exercises: Print the result of the specified operations

Sample Solution:

Java Code:

public class Main {

 public static void main(String[] args) {
   int w = -5 + 8 * 6;
   int x = (55 + 9) % 9;
   int y = 20 + (-3 * 5 / 8);
   int z = 5 + 15 / 3 * 2 - 8 % 3;

   System.out.print(w + "\n" + x + "\n" + y + "\n" + z);
  }
}

Flowchart:

Flowchart: Java exercises: Print the result of the specified operations

Java Code Editor:

Contribute your code and comments through Disqus.

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?



New Content: Composer: Dependency manager for PHP, R Programming