Arithmetic Operators in Java
The Java programming language supports various arithmetic operators for all floating-point and integer numbers. These operators are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo).
- Addition(+): This operator is a binary operator and is used to add two operands.
- Subtraction(-): This operator is a binary operator and is used to subtract two operands.
- Multiplication(*): This operator is a binary operator and is used to multiply two operands.
- 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 result.
- 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).
This Java program defines a class called Arithmetic which contains a main() method that performs basic arithmetic operations on two integer variables a and b. The program prints the results of the operations to the console using System.out.println() method. Here is the breakdown of the arithmetic operations performed in the program:
- The + operator is used to add the values of a and b. The result is printed using System.out.println() method with a string message "Addition: ".
- The - operator is used to subtract the value of b from a. The result is printed using System.out.println() method with a string message "Subtraction: ".
- The * operator is used to multiply the values of a and b. The result is printed using System.out.println() method with a string message "Multiplication: ".
- The / operator is used to divide the value of a by b. The result is printed using System.out.println() method with a string message "Division: ".
- The % operator is used to calculate the remainder when a is divided by b. The result is printed using System.out.println() method with a string message "Modulus: ".
Source Code
//Arithmetic Operators
public class Arithmetic {
public static void main(String args[])
{
int a=123,b=10;
System.out.println("Addition : "+(a+b));
System.out.println("Subtraction : "+(a-b));
System.out.println("Multiplication : "+(a*b));
System.out.println("Division : "+(a/b));
System.out.println("Modulus : "+(a%b));
}
}
Output
Addition : 133
Subtraction : 113
Multiplication : 1230
Division : 12
Modulus : 3
To download raw file
Click Here