Arithmetic Operators In Java With Example Program

Various type of operator in java


Arithmetic-Operators-In-Java


There are seven types of Arithmetic Operator In Java

No Operator USD Description
1      + A + B Addition
2     - A - B Subtraction
3     A * B Multiplication
4     / A / B Division
5     % A % B Modulus
6     ++ A++ Increment
7     – – A-- Decrement

1) Addition operator


  • Adition operator is used in the addition of two and more value. 
  • Another use is concatenation in string  
Example
public class AdditionOperator {
public static void main(String[] args) {

int a= 10,b=20;
float c=20.2f,d=20.7f;
double e=20.3f,f=30.7d;
 
 System.out.println("addition of a and b "+(a+b));
 System.out.println("addition of a and d "+(c+d));
 System.out.println("addition of e and f "+(e+f));
 
 //addition between int and double
 System.out.println("addition of a and e "+(a+e));
 
 }
}

output
addition of a and b 30
addition of a and d 40.9
addition of e and f 50.99999923706055
addition of a and e 30.299999237060547

2) subtraction operator

  • subtract right-hand operand from the left-hand operand
  • subtraction is use two value should be subtracted

Example
public class subtraction {
 public static void main(String[] args) {
  
  int a=20;
  int b= 25;
  float c= 20.5f;
  double d=33/5d;
  
  System.out.println("subtraction between a and b  "+(a-b));
  System.out.println("subtraction between a and c  "+(a-c));
  System.out.println("subtraction between c and d  "+(d-c));
 }

}

output
subtraction between a and b  -5
subtraction between a and c  -0.5
subtraction between c and d  -13.9


3) Multiplication operator

  • Multiplication is also used in the multiplies two or more value

Example

public class Multiplication {
 
 public static void main(String[] args) {
 
  int a=30,b=89;
  float c=28.8f;
  double d=98.23d;
  
  System.out.println("Multiplication of a and b "+(a*b));
  System.out.println("Multiplication of b and c "+(b*c));
  System.out.println("Multiplication of c and d "+(c*d));
  
 }
}
output
Multiplication of a and b 2670
Multiplication of b and c 2563.2
Multiplication of c and d 2829.0239250564578


4) Division operator

  • Divide two or more value
  • divide by left-hand operand by right-hand operand 

Example
public class Division {
 public static void main(String[] args) {
  
  int a= 10,b=20;
  float c=25.5f;
  double d=34.53d;
  
  System.out.println("Division between a and b "+(a/b));
  System.out.println("Division between a and c "+(c/a));
  System.out.println("Division between c and d "+(b/c));
  
 }

}
Output
Division between a and b 0
Division between a and c 2.55
Division between c and d 0.78431374


5) modulus operator

  • Divide by left-hand operand by right-hand operand and return reminder

Example
public class Modualus {
 public static void main(String[] args) {
  
  int a= 5,b=20;
  float v=25.f;
  double m = 34.53d;
  
  System.out.println("modulus of a and b "+(a%b));
  System.out.println("modulus of b and a "+(b%a));
  //float and int datatype
  System.out.println("modulus of b and v "+(b%v));
  //double and intdata type 
  System.out.println("modulus of a and m "+(a%m));
 
 }
}
output
modulus of a and b 5
modulus of b and a 0
modulus of b and v 20.0
modulus of a and m 5.0


6) increment operator

  • increase the operand value of 1

Example
public class Incrementoperator {
 
 public static void main(String[] args) {
  
  int i=555;
  //before increment i value
  System.out.println("Before increment value of i "+i);
  
  i++;
  //after increment i value
  System.out.println("After increment value of i  "+i);
  
 }
}

output
Before increment value of i 555
After increment value of i  556


7) Decrement operator

  • Decrease the value of 1

Example
public class Decrementoperator {
 
 public static void main(String[] args) {
  
  int a=2222;
  //before decrement value of a
  System.out.println("before decrement value of a "+a);
  
  a--;
  System.out.println("after decrement value of a "+a);

 }

}

output
before decrement value of a 2222
after decrement value of a 2221

above all details in the Arithmetic Operators In Java

Post a Comment

Previous Post Next Post