Arithmetic operators in java
Introduction
The four operators arithmetic operators are:
+ (used for addition)
– (used for subtraction)
* (used for multiplication)
/ (used for division)
Java Code listing
package com.testingdocs.demo; //ArithmeticOperatorDemo.java //www.TestingDocs.com public class ArithmeticOperatorDemo { public static void main(String[] args) { int x=24; int y=12; int sum=0; int substract=0; int product=0; float division=0.0f; System.out.println("x =" +x); System.out.println("y =" +y); //add two numbers sum=x+y; System.out.println("Sum of two numbers x + y is =" +sum); //substract two numbers substract=x-y; System.out.println("Substraction of x-y is =" +substract); //multiply two numbers product = x*y; System.out.println("Product of two numbers x*y is =" +product); //division of two numbers division = (float) (x/y); System.out.println("Division of two numbers x/y is =" +division); } }
Sample Output
x =24
y =12
Sum of two numbers x + y is =36
Substraction of x-y is =12
Product of two numbers x*y is =288
Division of two numbers x/y is =2.0
Screenshot