Arithmetic operators in java

Sandipan Kr Bag Apr 28, 2024
Arithmetic operators in java

Arithmetic Operators :

You must be friendly arithmetic expressions in mathematics like  A-B. In this expression A and B are Operands and the subtraction sign is the operator. The same terminology is also used here in the programming language.

Type of Arithmetical Operators :

  1. Binary Arithmetical Operators
  2.  Unary Arithmetical Operators

Binary Arithmetical Operators :

OperationOperator (Symbol)SyntaxResult if a= 22, b=5
Addition+a+b27
Subtraction-a-b17
Multiplication*a*b110
Division/a/b4
Modulus/Remainder%a%b2
    

 Unary Arithmetical Operators :

OperatorUseDescription
++ Post - incrementA++

Post-increment: The value is assigned before the increment is made , e.g. 

A=1

B=A++

Then B will hold 1 and A will Hold 2

– Post-IncrementA--

Post-decrement: The value is assigned before the decrement is made , e.g. : 

A=1;

B=A--;

Then B will hold 1 and A will hold 0.

++ pre-Increment++A

Pre-Increment:  The value is assigned after the increment is made. e.g : 

A=1;

B=++A;

Then B will hold 2 and A will hold 2.

– pre-decrement--A

Pre-decrement: the value is assigned after the decrement is made, e.g: 

A=1;

B=--A;

Then B will hold 0 and will hold 0.

 

Solved Example :

int a=7,b=5;c;

c = ++a*4+b++;

System.out.println(c);

Ans: 37

 

int x=5; y=4; z ;

z = x*y++  +  ++x/y;

System.out.println(z);

Ans :21

 

int p=6,q=15, r;

r = (p*q) + p/q;

System.out.println(r);

Ans :10

 

int a=5, b=10, 

a = a+b++ + --b+b

System.out.println(a);

Ans :35

 

int m = 10, n=12, p;

p = (m++ *n++)+ --n/2;

System.out.println(p);

Ans :126

 

Order of precedence :

In Java, as in many programming languages, expressions within parentheses are evaluated first, followed by multiplication (*), division (/), and modulus (%) operators, and then addition (+) and subtraction (-) operators.

  1.            →      ()
  2.            →       * , % , /
  3.            →        + , -

Example : 

10+20/5-3*2

5*(10+5)+10-5

(6 + 2) * 3 - 5

10 - 3 * (5 + 2)

4 * (7 - 3) / 2 + 5

(10 + 4) / (7 - 2) * 3

8 * (6 + 2) - 4 / 2

6 - (3 * 2 + 4) / 2

(10 + 4) / (7 - 2) * 3

 

 

Share this article

sndp bag

Sandipan Kr Bag

I'm a dedicated full-stack developer, entrepreneur, and the proud owner of ocec.org.in , hailing from the vibrant country of India. My passion lies in creating informative tutorials and sharing valuable tips that empower fellow artisans in their journey. With a deep-rooted love for technology, I've been an ardent enthusiast of PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, Codeigniter, and Bootstrap from their earliest days. My philosophy revolves around the values of hard work and unwavering consistency, driving me to continuously explore, create, and share my knowledge with the tech community.

* Hire Me

0 Comments

Load more Comments

Post a Comment

Hint: Please enter between 80 - 300 characters.
ocec.org.in

helllo