Thursday 27 September 2012

Java Lesson 2 "Working with Operators"

The Java programming language has includes five simple arithmetic operators like are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). The following table summarizes the binary arithmetic operators in the Java programming language.

The relation operators in Java are: ==, !=, <, >, <=, and >=. The meanings of these operators are:

Use Returns true if
op1 + op2 op1 added to op2
op1 - op2 op2 subtracted from op1
op1 * op2 op1 multiplied with op2
op1 / op2 op1 divided by op2
op1 % op2 Computes 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.

Program:

public class Arithmeticoperator {


public static void main(String[] args) {

int i=10;
int j=20;
double x=10.5;
double y=20.5;

System.out.println("addition");
System.out.println("i+j = "+(i+j));
System.out.println("x+y = "+(x+y));

System.out.println("subtraction");
System.out.println("i-j ="+(i-j));
System.out.println("x-y ="+(x-y));

System.out.println("multiplication");
System.out.println("i*j ="+(i*j));
System.out.println("x*j ="+(x*j));

System.out.println("division");
System.out.println("i/j ="+(i/j));
System.out.println("x/y ="+(x/y));

System.out.println("reminder");
System.out.println("i%j ="+(i/j));
System.out.println("x%j ="+(x%y));

}

}

Output:

addition
i+j = 30
x+y = 31.0
subtraction
i-j =-10
x-y =-10.0
multiplication
i*j =200
x*j =210.0
division
i/j =0
x/y =0.5121951219512195
reminder
i%j =0
x%j =10.5

0 comments:

Post a Comment

Blogroll

free counters