Math Function in Java
Introduction :
The java math Methods provides a lot of built in methods that are used to manipulate mathematical in java. We can perform operations on string object such as sin(), cin(), tan(), etc.
Let's use some important methods of string class.
1. Java sin() function
- It return the sine of given value.
class Mathsin
{
public static void main(String[] args){
System.out.println(Math.sin(2));
}
}
----OUTPUT----
0.9092974268256817
2. Java cos() function
- It return the cosine of given value.
class Mathcos
{
public static void main(String[] args)
{
System.out.println(Math.cos(2));
}
}
----OUTPUT----
-0.4161468365471424
3. Java tan() function
- It return the tangent of given value.
class Mathtan
{
public static void main(String[] args)
{
System.out.println(Math.(2));
}
}
----OUTPUT----
-2.185039863261519
4. Java pow() function
- It is used to calculate the value of the first argument raised to the power of the second argument.
- Syntax:----pow(base,power)
class Mathpow
{
public static void main(String[] args)
{
System.out.println(Math.pow(2.0,3.0));
//pow(base,power)
}
}
----OUTPUT----
8.0
Guys same as use lots of function of math using in java.
// 5.sqrt() function
// It is used to calculate the square root of given input.
class Mathsqr
{
public static void main(String[] args)
{
System.out.println(Math.sqrt(9));//3.0
System.out.println(Math.sqrt(8));//2.8284271247461903
}
}
// 6.cbrt() function
// It is used to calculate the cube root of given input.
class Mathcbr
{
public static void main(String[] args)
{
System.out.println(Math.cbrt(27));//3.0
System.out.println(Math.cbrt(12));//2.2894284851066637
}
}
// 7.exp() function
// It is used to calculate the exponential value of given input.
class Mathexp
{
public static void main(String[] args)
{
System.out.println(Math.exp(2));//7.38905609893065
System.out.println(Math.exp(0));//1.0
}
}
// 8.log() function
// Used to find natural logarithm of input value.
class Mathlog
{
public static void main(String[] args)
{
System.out.println(Math.log(2));//0.6931471805599453
}
}
// 9. log10() function
// Used to find logarithm of input value to the base of 10.
class Mathlog1
{
public static void main(String[] args)
{
System.out.println(Math.log10(2));//0.3010299956639812
}
}
// 10.max() function
// Returns the maximum value from two number.
class Mathmax
{
public static void main(String[] args)
{
System.out.println(Math.max(1.8, 8.5));
}
}
// 11 min()
// Returns the minimum value from two number.
class Mathmin
{
public static void main(String[] args)
{
System.out.println(Math.min(1.8, 8.5));
}
}
// 12.abs() functions
// Returns the absolute value from two number.
// It always returns positive value.
class Mathabs
{
public static void main(String[] args)
{
System.out.println(Math.abs(-5));//5
System.out.println(Math.abs(5));//5
}
}
// 13.floor() functions
// Returns the largest integer value which is less than or equal to input value.
class Mathflo
{
public static void main(String[] args)
{
System.out.println(Math.floor(3.2));//3
System.out.println(Math.floor(3.5));//3
System.out.println(Math.floor(3.9));//3
}
}
// 14 ceil()
// Returns the smallest integer value which is greater than or equal to input value.
class Mathcei
{
public static void main(String[] args)
{
System.out.println(Math.ceil(3.2));//4
System.out.println(Math.ceil(3.5));//4
System.out.println(Math.ceil(3.9));//4
}
}
// 15. round() functions
// It returns the nearest integer value of the value(float,long etc) passed to this function.
class Mathron
{
public static void main(String[] args)
{
System.out.println(Math.round(3.2));//3
System.out.println(Math.round(3.5));//4
System.out.println(Math.round(3.9));//4
}
}
// 16.rint() functions
class Mathrin
{
public static void main(String[] args)
{
System.out.println(Math.rint(3.2));//3
System.out.println(Math.rint(3.5));//4
System.out.println(Math.rint(3.9));//4
}
}
// 17.random() finction
// It returns random value/number.
class Mathran
{
public static void main(String[] args)
{
System.out.println(Math.random());
System.out.println(Math.random());
System.out.println(Math.random());
}
}
// 18. toDegrees() function
// It converts the given value into degrees.
class MathDeg
{
public static void main(String[] args)
{
System.out.println(Math.toDegrees(10));
}
}
// 19.toRadians() function
// It converts the given value into radians.
class Matrad
{
public static void main(String[] args)
{
System.out.println("Radians of 10="+Math.toRadians(10));
}
}
// 20. compareTo() function
// It compares two numbers.
// Returns 1 if comparable value is smaller than given value
// Returns 0 if comparable value is equal to given value
// Returns -1 if comparable value is greater than given value
class Mathcom
{
public static void main(String[] args)
{
Integer x=10;
System.out.println(x.compareTo(5));//1
System.out.println(x.compareTo(10));//0
System.out.println(x.compareTo(15));//-1
}
}
// 21. equals() function
// It compares two numbers and returns boolean value(True/False).
// Returns True if both numbers are same.
// Returns False if both numbers are different.
class Mathequ
{
public static void main(String[] args)
{
Integer x=10;
System.out.println(x.equals(5));//false
System.out.println(x.equals(10));//true
}
}
// 22. parseInt() functions
// It is used to convert string into integer.
class Mathpar
{
public static void main(String[] args)
{
String x="10";
System.out.println(x+10);//1010 because x is string
System.out.println(Integer.parseInt(x)+10);//20 here x is integer
System.out.println(Float.parseFloat(x)+10);//20.0 here x is float
System.out.println(Double.parseDouble(x)+10);//20.0 here x is double
}
}
So friends i hope like this post then don't forget follow me.
Comments