Hello Friends,
Hope you will like this article.
In this article we will be familiar with maths functions of
java. Here example is given and by running this example code to your pc, you
will easily get and be familiar with all these functions.
public class MathsFunctions {
public static void
main(String args[]){
int i = 2;
int j = -4;
double x = 19.8;
double y = 0.38;
System.out.println("i is " + i);
System.out.println("j is " + j);
System.out.println("x is " + x);
System.out.println("y is " + y);
/*
Math.round(variable) Function
-----------------------------
It gives
rounding value of variable, depending upon value of variable is
less than or
greater than 0.50
*/
System.out.println("Value
of Math.round(x) is : "+Math.round(x));
System.out.println("Value of Math.round(y) is :
"+Math.round(y));
//
----------------------------------------------------------------------------
/*
Math.abs(variable) Function
---------------------------
It gives integer
value of variable, if it is negatie than it converts
to positive
number
*/
System.out.println("Value of Math.abs(i) is : " +
Math.abs(i));
System.out.println("Value of Math.abs(j) is : " +
Math.abs(j));
System.out.println("Value of Math.abs(x) is : " +
Math.abs(x));
System.out.println("Value of Math.abs(y) is : " +
Math.abs(y));
//
---------------------------------------------------------------------------
/*
Math.min(var1,var2) Function
----------------------------
It gives minimum
number value between the two variables
*/
System.out.println("Minimum between " +i+" and j "
+j+ " is : " + Math.min(i,j));
System.out.println("Minimum between " +x+" and j "
+y+ " is : " + Math.min(x,y));
System.out.println("Minimum between " +i+" and j "
+x+ " is : " + Math.min(i,x));
System.out.println("Minimum between " +y+" and j "
+j+ " is : " + Math.min(y,j));
// ---------------------------------------------------------------------------
/*
Math.max(var1,var2) Function
----------------------------
It gives maximum
number value between the two variables
*/
System.out.println("Maximum between " +i+" and j "
+j+ " is : " + Math.max(i,j));
System.out.println("Maximum between " +x+" and j "
+y+ " is : " + Math.max(x,y));
System.out.println("Maximum between " +i+" and j "
+x+ " is : " + Math.max(i,x));
System.out.println("Maximum between " +y+" and j "
+j+ " is : " + Math.max(y,j));
//
---------------------------------------------------------------------------
/*
Math.ceil(variable)
-------------------
It gives integer
value greater than or equal to the number
*/
System.out.println("Value of Math.ceil(i) is : " +
Math.ceil(i));
System.out.println("Value of Math.ceil(j) is : " +
Math.ceil(j));
System.out.println("Value of Math.ceil(x) is : " +
Math.ceil(x));
System.out.println("Value of Math.ceil(y) is : " +
Math.ceil(y));
//
---------------------------------------------------------------------------
/*
Math.floor(variable)
-------------------
It gives integer
value less than or equal to the number
*/
System.out.println("Value of Math.floor(i) is : " +
Math.floor(i));
System.out.println("Value of Math.floor(j) is : " +
Math.floor(j));
System.out.println("Value of Math.floor(x) is : " +
Math.floor(x));
System.out.println("Value of Math.floor(y) is : " +
Math.floor(y));
//
---------------------------------------------------------------------------
/*
Math.pow(var1,var2)
-------------------
It gives var1
raised to var2 value
*/
System.out.println("Value
of pow(3.0, 2.0) is : " +
Math.pow(3.0,2.0));
System.out.println("Value of pow(5.0, 1.5) is : " + Math.pow(5.0,1.5));
System.out.println("Value of pow(3, -1) is : " + Math.pow(3,-1));
// ---------------------------------------------------------------------------
/*
Math.sqrt(variable)
-------------------
It gives square
value root of variable
*/
for (i=0; i <
10; i++) {
System.out.println(
"The
square root of " + i + " is " + Math.sqrt(i));
}
//
---------------------------------------------------------------------------
/*
Math.random(variable)
-------------------
It gives random
number between o to 9
*/
System.out.println("Example of Random Number : " +
Math.random());
System.out.println("Example of another Random Number : " +
Math.random());
}
}