logo image

Basic Mathematical operations on Integer numbers & Floating point numbers in Java



This is a simple Java program shows you the basic Mathematical operations using Integers & Floating point numbers

public class BasicMaths {

	public static void main(String[] args) {
		int i = 4;
		int j = 2;
		System.out.println("sum:" + (i + j));
		System.out.println("sub:" + (i - j));
		System.out.println("mul:" + (i * j));
		System.out.println("div:" + (i / j));
		System.out.println("remainder:" + (i % j));

		float f1 = 1.5f;
		float f2 = 1.2f;
		System.out.println("sum:" + (f1 + f2));
		System.out.println("sub:" + (f1 - f2));
		System.out.println("mul:" + (f1 * f2));
		System.out.println("div:" + (f1 / f2));
		System.out.println("remainder:" + (f1 % f2));
	}
}

Output

sum:6
sub:2
mul:8
div:2
remainder:0
sum:2.7
sub:0.29999995
mul:1.8000001
div:1.25
remainder:0.29999995