Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe