Last Updated: 09 September, 2022
Hello, on this tutorial page, we will briefly learn about an operation and its type in Java, followed by a main discussion about arithmetic operators and types of arithmetic operators with example.
Operators are special symbols that perform the specified operations. Each operator is responsible for performing some specific operation on variables or values and returning a result. The operators are classified and listed according to precedence order.
Java supports the following types of operators:
Arithmetic Operators are used to perform mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Arithmetic operators consist of many unary and binary operators that can be applied to one or two operands. The operands may be values or variables.
Let's understand arithmetic operators with the help of the given table below.
Here, a=20, b=10
Operator | Name | Description | Example | Output |
---|---|---|---|---|
+ | Addition | Addition of two values or variables | a + b | 30 |
- | Subtraction | Subtraction of two values or variables | a - b | 10 |
* | Multiplication | Multiplication of two values or variables | a * b | 200 |
/ | Division | Division of two values or variables | a / b | 2 |
% | Modulus | Remainder of division of two values or variables | a % b | 0 |
Output
30
10
200
2
0
In the below example, we have written a simple calculator program using arithmetic operations and switch cases that performs addition (+), subtraction (-), multiplication (*), and division (/) operations based on the user's input.
Let's see the Java program implementation.
If we run the program with all possible inputs, we will get the following output:
Output 1 for add operation (+)
Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: + Enter first number: 10.50 Enter second number: 20.55 Addition output: 31.05
Output 2 for subtract operation (-)
Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: - Enter first number: 200 Enter second number: 50.55 Subtraction output: 149.45
Output 3 for multiplication operation (*)
Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: * Enter first number: 100.1 Enter second number: 5 Multiplication output: 500.5
Output 4 for division operation (/)
Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: / Enter first number: 400 Enter second number: 5 Division output: 80.0
Output 5 for invalid input
Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: # Enter first number: 10 Enter second number: 20 Please enter a valid operator
Here, with all five possible inputs, we ran this program and showed the output above.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com