Java Maths Functions

  1. E, PI, round(), abs(), ceil() & floor() Math function in Java
  2. exp(), IEEEremainder(), min() & max() Math function in Java
  3. pow(), random(), rint() & sqrt() Math function in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. E, PI, round(), abs(), ceil() & floor() Math function in Java


Math_Ex1.java

public class Math_Ex1 {
	
	Math_Ex1() {
		
		System.out.println("The Math Class");
	}
	
	void display_E() {
		
		System.out.println("\nThe value of E is : " + Math.E);
		
		System.out.println("The round value of E is : " + Math.round(Math.E));
	}
	
	void display_PI() {
		
		System.out.println("\nThe value of PI is : " + Math.PI);
		
		System.out.println("The round value of PI is : " + Math.round(Math.PI));
	}
	
	void display_abs() {
		
		System.out.println("\nThe Absolute value of E is : " + Math.abs(Math.E));
		
		System.out.println("The Absolute value of PI is : " + Math.abs(Math.PI));
	}
	
	void display_ceil() {
		
		System.out.println("\nThe ceil value of E is : " + Math.ceil(Math.E));
		
		System.out.println("The ceil value of PI is : " + Math.ceil(Math.PI));
		
		System.out.println("The ceil value of 12.1 : " + Math.ceil(12.1));
		System.out.println("The ceil value of 12.0 : " + Math.ceil(12.0));
	}
	
	void display_floor() {
		
		System.out.println("\nThe floor value of E is : " + Math.floor(Math.E));
		
		System.out.println("The floor value of PI is : " + Math.floor(Math.PI));
		
		System.out.println("The floor value of 12.9 : " + Math.floor(12.9));
		System.out.println("The floor value of 12.0 : " + Math.floor(12.0));
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Math_Ex1 obj = new Math_Ex1();
		
		obj.display_E();
		obj.display_PI();
		obj.display_abs();
		obj.display_ceil();
		obj.display_floor();
	}
}

Sample Output

The Math Class

The value of E is : 2.718281828459045
The round value of E is : 3

The value of PI is : 3.141592653589793
The round value of PI is : 3

The Absolute value of E is : 2.718281828459045
The Absolute value of PI is : 3.141592653589793

The ceil value of E is : 3.0
The ceil value of PI is : 4.0
The ceil value of 12.1 : 13.0
The ceil value of 12.0 : 12.0

The floor value of E is : 2.0
The floor value of PI is : 3.0
The floor value of 12.9 : 12.0
The floor value of 12.0 : 12.0




B. exp(), IEEEremainder(), min() & max() Math function in Java


Math_Ex2.java

public class Math_Ex2 {
	
	Math_Ex2() {
		
		System.out.println("The Math Class");
	}
	
	void display_exp() {
		
		System.out.println("\nExponent number powered by " +
				"the argument (1) : " + Math.exp(1));
		
		System.out.println("Exponent number powered by " +
				"the argument (2) : " + Math.exp(2));
	}
	
	void display_IEEEremainder() {
		
		System.out.println("\nThe reminder is : " + Math.IEEEremainder(10, 2));
		
		System.out.println("The reminder is : " + Math.IEEEremainder(10, 3));
	}
	
	void display_max() {
		
		System.out.println("\nThe Maximum value is : " + Math.max(10, 20));
		
		System.out.println("The Maximum value is : " + Math.max(12.3654, 12.3655));
	}
	
	void display_min() {
		
		System.out.println("\nThe Minimum value is : " + Math.min(10, 20));
		
		System.out.println("The Minimum value is : " + Math.min(12.3654, 12.3655));
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Math_Ex2 obj = new Math_Ex2();
		
		obj.display_exp();
		obj.display_IEEEremainder();
		obj.display_max();
		obj.display_min();
	}
}

Sample Output

The Math Class

Exponent number powered by the argument (1) : 2.7182818284590455
Exponent number powered by the argument (2) : 7.38905609893065

The reminder is : 0.0
The reminder is : 1.0

The Maximum value is : 20
The Maximum value is : 12.3655

The Minimum value is : 10
The Minimum value is : 12.3654




C. pow(), random(), rint() & sqrt() Math function in Java


Math_Ex3.java

public class Math_Ex3 {
	
	Math_Ex3() {
		
		System.out.println("The Math Class");
	}
	
	void display_pow() {
		
		System.out.println("\nThe Power : " + Math.pow(3, 2));
		
		System.out.println("The Power : " + Math.pow(12.34, 2.36));
	}
	
	void display_random() {
		
		System.out.println("\nThe Random number is : " + Math.random());
		
		System.out.println("The Random number is : " + Math.random());
	}
	
	void display_rint() {
		
		System.out.println("\nThe Closest Int value is : " + Math.rint(12.444));
		
		System.out.println("The Closest Int value is : " + Math.rint(12.555));
	}
	
	void display_sqrt() {
		
		System.out.println("\nThe Square root value is : " + Math.sqrt(4));
		
		System.out.println("The Square root value is : " + Math.sqrt(625));
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Math_Ex3 obj = new Math_Ex3();
		
		obj.display_pow();
		obj.display_random();
		obj.display_rint();
		obj.display_sqrt();
	}
}

Sample Output

The Math Class

The Power : 9.0
The Power : 376.2736245547869

The Random number is : 0.09965686489786707
The Random number is : 0.3308343498651476

The Closest Int value is : 12.0
The Closest Int value is : 13.0

The Square root value is : 2.0
The Square root value is : 25.0






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com