Tuesday, 17 September 2013

JAVA Tutorial PART 2 - Arithmetic Operators

JAVA Tutorial PART 2 – Arithmetic Operators


 1.     Java Arithmetic Operators

2.     Java Assignment Operators

3.     Java Increment and Decrement Operators

4.     Java Relational Operators

5.     Java Boolean Operators

6.     Java Conditional Operators



Java Arithmetic Operators

The Java Programming Language has includes Five Simple Arithmetic Operators like are

+ (ADDITION),

- (SUBTRACTION),

* (MULTIPLICATION),

/ (DIVISION),

% (MODULO).

The following table summarizes the Binary Arithmetic Operators in the Java programming language.













USE     RETURNS TRUE IF
OP1 + OP2OP1 Added To OP2
OP1 – OP2OP2 Subtracted From OP1
OP1 * OP2OP1 Multiplied With OP2
OP1 / OP2OP1 Divided By OP2
OP1 % OP2Computes the Remainder of Dividing OP1 By OP2
The following java program, ArithmeticProg , defines Two Integers and Two Double-precision Floating-Point Numbers and uses the Five Arithmetic Operators to Perform Different Arithmetic Operations. This Program also uses + to Concatenate Strings. The Arithmetic Operations are Shown in Boldface.

 Example Source Code:

public class ArithmeticProg

{

public static void main(String[] args)

{

//A Few Numbers

int i = 10;

int j = 20;

double x = 10.5;

double y = 20.5;

//Adding Numbers

System.out.println(“Adding”);

System.out.println(” i + j = ” + (i + j));

System.out.println(” x + y = ” + (x + y));

 //Subtracting Numbers

System.out.println(“Subtracting”);

System.out.println(” i – j = ” + (i – j));

System.out.println(” x – y = ” + (x – y));

 //Multiplying Numbers

System.out.println(“Multiplying”);

System.out.println(” i * j = ” + (i * j));

System.out.println(” x * y = ” + (x * y));

 //Dividing Numbers

System.out.println(“Dividing”);

System.out.println(” i / j = ” + (i / j));

System.out.println(” x / y = ” + (x / y));

 //Computing The Remainder Resulting

//From Dividing Numbers

System.out.println(“Modulus”);

System.out.println(” i % j = ” + (i % j));

System.out.println(” x % y = ” + (x % y));

}

}


JAVA Tutorial PART 2 - Arithmetic Operators
Categories: , ,

0 comments:

Post a Comment

    Followers