View Discussion Improve Article Save Article Like Article View Discussion Improve Article Save Article Like Article Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide. Here are a few types: This article explains all that one needs to know regarding Arithmetic Operators. These operators involve the mathematical operators that can be used to perform various simple or advanced arithmetic operations on the primitive data types referred to as the operands. These operators consist of various unary and binary operators that can be applied on a single or two operands. Let’s look at the various operators that Java has to provide under the arithmetic operators. Now let’s look at each one of the arithmetic operators in Java: 1. Addition(+): This operator is a binary operator and is used to add two operands. Syntax: num1 + num2Example: num1 = 10, num2 = 20 sum = num1 + num2 = 30
Outputnum1 = 10 num2 = 20 The sum = 30 2. Subtraction(-): This operator is a binary operator and is used to subtract two operands. Syntax: num1 - num2Example: num1 = 20, num2 = 10 sub = num1 - num2 = 10
Outputnum1 = 20 num2 = 10 Subtraction = 10 3. Multiplication(*): This operator is a binary operator and is used to multiply two operands. Syntax: num1 * num2Example: num1 = 20, num2 = 10 mult = num1 * num2 = 200
Outputnum1 = 20 num2 = 10 Multiplication = 200 4. Division(/): This is a binary operator that is used to divide the first operand(dividend) by the second operand(divisor) and give the quotient as a result. Syntax: num1 / num2Example: num1 = 20, num2 = 10 div = num1 / num2 = 2
Outputnum1 = 20 num2 = 10 Division = 2 5. Modulus(%): This is a binary operator that is used to return the remainder when the first operand(dividend) is divided by the second operand(divisor). Syntax: num1 % num2Example: num1 = 5, num2 = 2 mod = num1 % num2 = 1
Outputnum1 = 5 num2 = 2 Remainder = 1
In order to continue enjoying our site, we ask that you confirm your identity as a human. Thank you very much for your cooperation. Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two numbers. Test Data:
Input first number: 125
Input second number: 24 Pictorial Presentation: Sample Solution: Java Code: Sample Output: Flowchart: Sample Solution: Java Code: Sample Output: Flowchart: Java Code Editor: Contribute your code and comments through Disqus. Previous: 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.
Array vs ArrayLists: The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size. Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array. At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects. Ref: https://bit.ly/3o8L2KH |