Arithmetic operators in java example

In this Example you will learn about different Arithmetic Operator in Java and their functioning.

Arithmetic Operators in Java

 

  • +     (Addition)
  • –      (Subtraction)
  • *     (Multiplication)
  • /     (Division)
  • %    (Modulus)
  • ++ (Increment)
  • —    (Decrement)

Example Code

 


import java.util.Scanner;

public class OperatorExample {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter first Value");
		int i = scanner.nextInt();
		System.out.println("Enter second Value");
		int j = scanner.nextInt();

		System.out.println("Values for different Arithmetic operator\n");
		System.out.println("i + j (Addition)      :"+(i+j));
		System.out.println("i - j (Subtraction)   :"+(i-j));
		System.out.println("i * j (Multiplication):"+(i*j));
		System.out.println("i / j (Divison)       :"+(i/j));
		System.out.println("i % j (Modulus)       :"+(i%j));
		System.out.println("i++ (Post Increment)  :"+(i++));
		System.out.println("++i (Pre Increment)   :"+(++i));
		System.out.println("--j (Pre Decrement)   :"+(--j));
		System.out.println("j-- (Post Decrement)  :"+(j--));

	}
}

	

Output of above programe would be


Enter first Value
10
Enter second Value
5
Values for different Arithmetic operator

i + j (Addition)      :15
i - j (Subtraction)   :5
i * j (Multiplication):50
i / j (Divison)       :2
i % j (Modulus)       :0
i++ (Post Increment)  :10
++i (Pre Increment)   :12
--j (Pre Decrement)   :4
j-- (Post Decrement)  :4

	
By | 2013-04-13T16:07:11+00:00 April 13th, 2013|Learn By Example|0 Comments

About the Author:

Leave A Comment