Java2all.com  

Arithmetic operators





Share This Topic -

Arithmetic operators in java:

EX.

 public class ArithmeticDemo
{
	public static void main(String[] args) 
	{
		System.out.println("..............Arithmetic operators.................");
		System.out.println();
		int a = 10;
		int b = 3;
		int c = 20;
		int d = 30;
		int e = 40;
		int f = 50;
		int i = 10;
		int j = 5;
		int m = 30;
		int l = 3;
		int o = 25;
		int v = 6;
		int z = 27;
		int r = 8;
		int add = a + b;
		int sub = a - b;
		int mul = a * b;
		int div = a / b;
		int mod = a % b;
		
		System.out.println("<-------Basic Arithmetic operators------->");
		System.out.println("Addition of a and b : a + b = 10 + 3 = "+add);
		System.out.println("Subtraction of a and b : a - b = 10 - 3 = "+sub);
		System.out.println("Multiplication of a and b : a * b = 10 * 3 = "+mul);
		System.out.println("Division of a and b : a / b = 10 / 3 = "+div);
		System.out.println("Modulus of a and b : a % b = 10 % 3 = "+mod);
		
		System.out.println();
		System.out.println("<-------Increment Operator------->");

		// c = 20, d = 30
		// post_incr = c++ => post_incr = c then c = c + 1 => post_incr = 20 and c = 20 + 1 = 21
		// pree_incr = ++d => d = d + 1 then pree_incr = d => d = 30 + 1 = 31 and pree_incr = 31
		

		System.out.println("Before Post-Increment of c = "+c);
		int post_incr = c++;
		System.out.println("Post-Increment of c : c++ = 20++ = "+post_incr);
		System.out.println("After Post-Increment of c = "+c);
		System.out.println("Before Pree-Increment of d = "+d);
		int pree_incr = ++d;
		System.out.println("Pree-Increment of d : ++d = ++30 = "+pree_incr);
		System.out.println("After Pree-Increment of d = "+d);
		
		System.out.println();
		System.out.println("<-------Decrement Operator------->");

		/* e = 40, f = 50
		 * post_decr = e-- => post_decr = e then e = e - 1 => post_decr = 40 and c = 40 - 1 = 39
		 * pree_decr = --f => f = f - 1 then pree_decr = f => f = 50 - 1 = 49 and pree_decr = 49
		*/

		System.out.println("Before Post-Decrement of c = "+e);
		int post_decr = e--;
		System.out.println("Post-Decrement of e : e-- = 40-- = "+post_decr);
		System.out.println("After Post-Decrement of e = "+e);
		System.out.println("Before Pree-Decrement of f = "+f);
		int pree_decr = --f;
		System.out.println("Pree-Decrement of f : --f = --50 = "+pree_decr);
		System.out.println("After Pree-Decrement of f = "+f);
	
		System.out.println();
		System.out.println("<-------Assignment Operators------->");
		System.out.println("Addition Assignment of a and b : a += b means 10+=3 => a = a + b => a = 10 + 3 = "+(a+=b));
		System.out.println("Subtraction Assignment of i and j : i -= j means 10-=5 => i = i - j => i = 10 - 5 = "+(i-=j));
		System.out.println("Multiplication Assignment of m and l : m *=l means 30*=3 => m = m * l => m = 30 * 3 = "+(m*=l));
		System.out.println("Division Assignment of o and v : o /= v means 25/=6 => o = o / v => o = 25 / 6 = "+(o/=v));
		System.out.println("Modulus Assignment of z and r : z %= r means 27%=8 => z = z % r => z  = 27 % 8 = "+(z%=r));
	}
}

Output :
 
..............Arithmetic operators.................
 
<-------Basic Arithmetic operators------->
Addition of a and b : a + b = 10 + 3 = 13
Subtraction of a and b : a - b = 10 - 3 = 7
Multiplication of a and b : a * b = 10 * 3 = 30
Division of a and b : a / b = 10 / 3 = 3
Modulus of a and b : a % b = 10 % 3 = 1
 
<-------Increment Operator------->
Before Post-Increment of c = 20
Post-Increment of c : c++ = 20++ = 20
After Post-Increment of c = 21
Before Pree-Increment of d = 30
Pree-Increment of d : ++d = ++30 = 31
After Pree-Increment of d = 31
 
<-------Decrement Operator------->
Before Post-Decrement of c = 40
Post-Decrement of e : e-- = 40-- = 40
After Post-Decrement of e = 39
Before Pree-Decrement of f = 50
Pree-Decrement of f : --f = --50 = 49
After Pree-Decrement of f = 49
 
<-------Assignment Operators------->
Addition Assignment of a and b : a += b means 10+=3 => a = a + b => a = 10 + 3 = 13
Subtraction Assignment of i and j : i -= j means 10-=5 => i = i - j => i = 10 - 5 = 5
Multiplication Assignment of m and l : m *=l means 30*=3 => m = m * l => m = 30 * 3 = 90
Division Assignment of o and v : o /= v means 25/=6 => o = o / v => o = 25 / 6 = 4
Modulus Assignment of z and r : z %= r means 27%=8 => z = z % r => z = 27 % 8 = 3