Home Java Master-java Arithmetic Operators
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Arithmetic Operators

Advertisement
Arithmetic Operators are used to perform some mathematical operations like addition, subtraction, multiplication, division, and modulo (or remainder).

Arithmetic Operators

     

Arithmetic Operators are used to perform some mathematical operations like addition, subtraction, multiplication, division, and modulo (or remainder). These are generally performed on an expression or operands. The symbols of arithmetic operators are given in a table:

Symbol Name of the Operator Example
 + Additive Operator   n =  n + 1;
 - Subtraction Operator  n =  n - 1;
 * Multiplication Operator   n =  n * 1;
 / Division Operator  n = n / 1;
 % Remainder Operator  n = n % 1;

The "+" operator can also be used to concatenate (to join) the two strings together. For example:

String str1 = "Concatenation of the first";
String str2 = "and second String";
String result = str1 + str2;

 The variable "result" now contains a string "Concatenation of the first and second String".

Lets have one more example implementing all arithmetic operators:

class ArithmeticDemo{
  public static void main(String[] args) {
  int x = 4;
  int y = 6;
  int z = 10;
  int rs = 0;

  rs = x + y;
  System.out.println("The addition of (x+y):"+ rs);

  rs  = y - x;
  System.out.println("The subtraction of (y-x):"+ rs);

  rs = x * y;
  System.out.println("The multiplication of (x*y):"+ rs);

  rs = y / x;
  System.out.println("The division of (y/x):"+ rs);

  rs = z % y;
  System.out.println("
The remainder of (z%x):"+ rs);

  rs = x + (y * (z/x));
  System.out.println("The result is now :"+ rs);
  }
}

Output of the Program:

C:\nisha>javac ArithmeticDemo.java

C:\nisha>java ArithmeticDemo
The addition of (x + y): 10
The subtraction of (y - x): 2
The multiplication of (x * y): 24
The division of (y / x): 1
The remainder of (z % x): 4
The result is now : 16

Download this program

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Ask Questions?    Discuss: Arithmetic Operators  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com