Friday, 24 August 2012

[Operator] Contoh Program Operator Aritmatika

Berikut ini adalah contoh program dalam penggunaan operator-operator Aritmatika:



public class aritmatikaDemo
{
public static void main(String[] args)
{
//sedikit angka
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
System.out.println("Variable values...");
System.out.println(" i = " + i);
System.out.println(" j = " + j);
System.out.println(" x = " + x);
System.out.println(" y = " + y); //penjumlahan angka
System.out.println("Adding...");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));
//pengurangan angka
System.out.println("Subtracting...");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));
//perkalian angka
System.out.println("Multiplying...");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));
//pembagian angka
System.out.println("Dividing...");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));
//menghitung hasil modulus dari pembagian
System.out.println("Computing the remainder...");
System.out.println(" i % j = " + (i % j));
System.out.println(" x % y = " + (x % y));
//tipe penggabungan
System.out.println("Mixing tipes...");
System.out.println(" j + y = " + (j + y));
System.out.println(" i * x = " + (i * x));
}
}


Berikut ini adalah output program,


Variable values...
i = 37
j = 42
x = 27.475
y = 7.22
i + j = 79
Adding...
x + y = 34.695
Subtracting...
i - j = -5
x - y = 20.255
Multiplying...
i * j = 1554
x * y = 198.37
Dividing...
i / j = 0
x / y = 3.8054
Computing the remainder...
i % j = 37
x % y = 5.815
Mixing tipes...
j + y = 49.22
i * x = 1016.58




Catatan: 
Ketika integer dan floating-point number digunakan sebagai operand untuk
operasi aritmatika tunggal, hasilnya berupa floating point. Integer adalah converter secara
implisit ke bentuk angka floating-point sebelum operasi berperan mengambil tempat.


Sumber : J.E.N.I

0 comments:

Post a Comment