Arithmetic Operators in Java with Example.

In the last tutorial, we have learned how to declare and assign value to the variable, how to declare data types and operators precedence in java. In this tutorial, we will learn Arithmetic Operators in Java


Arithmetic Operators in Java:

Arithmetic Operators in Java are used to perform mathematical operations. The operands of the Arithmetic Operators must be a numeric value. We cannot use them on boolean types(i.e. true/false), but we can use them on char types. The basic Arithmetic Operators in Java are: 

1. Addition Operator - It can be used to add the two values.
2. Subtraction Operator - It can be used to subtract the two values.
3. Multiplication Operator - It can be used to multiply the two values.
4. Division Operator - It can be used to divide the two values.
5. Modulus Operator - It returns the remainder of a division operation.


Example of Arithmetic Operators in Java:


class ArithmeticOperators{
	public static void main(String args[]){
		int a = 50, b = 20;
		
		// Perform addition operation.
		System.out.println("Addition Operation is: ");
		System.out.println("Addition of " +a+ " and " +b+ " is: " +(a+b));
		
		// perform substraction operation.
		System.out.println("Substraction Operation is: ");
		System.out.println("Substraction of " +a+ " and " +b+ " is: " +(a-b));
		
		// perform multiplication operation.
		System.out.println("Multiplication Operation is: ");
		System.out.println("Multiplication of " +a+ " and " +b+ " is: " +(a*b));
		
		// perform divison operation.
		System.out.println("Division Operation is: ");
		System.out.println("Division of " +a+ " and " +b+ " is: " +(a/b));
		
		// perform modulus operation.
		System.out.println("Modulus Operation is: ");
		System.out.println("Modulus of " +a+ " and " +b+ " is: " +(a%b));
	}
}


Output:

Arithmetic Operators in Java


You may also like these tutorials:

1. Top 100 Java Interview questions and answers.
2. Java Hello World program - Java first program.
3. How to set path in Java- Java environment set up.
4. Exception Handling in java with example.
5. Comparable vs Comparator interface in java with example.
6. Java Collection complete tutorial
7. Inheritance in java with example.
Arithmetic Operators in Java with Example. Arithmetic Operators in Java with Example. Reviewed by Prashant Srivastava on December 14, 2019 Rating: 5

No comments:

Powered by Blogger.