Mathematical functions in Java

Java provides various mathmetical functions like cos, sin, sqrt, tan etc.

These functions are used through Math class defined in Java.lang package. The function should be used with the following syntax:-

Math.function-name();

For example:     double y=Math.sqrt(x);

Various Math functions are……

Sin(n)    

Sqrt(n)                     Tan(n)

Pow(n,m)           Min(n,m)                 Max(n,m)

Abs(n)                  Cos(n)

Example…..

class sqrroot

{

public void root()

{

double a,b;

a=625;

b=Math.sqrt(a);   //returns the square root of a to b

System.out.println("Square root="+b);

}

public void power()

{

double a,b,c;

a=2.5;

b=3.0;

c=Math.pow(a,b); //returns a raised to power b

System.out.println("Power value is=="+c);

}

public void get()

{

double a,b;

a=60*22/7*(1/180);

b=Math.cos(a); //returns the cosine of a

System.out.println("Cosine of the angle="+b);

}

public void sine()

{

double a,b;

a=30.0*22/7*(1/180);

b=Math.sin(a);  //returns the sine of a

System.out.println("Sin of the angle="+b);

}

}

class mathh

{

public static void main(String aa[])

{

sqrroot ob=new sqrroot();

ob.root();

ob.power();

ob.get();

ob.sine();

}

}

 

Mathmatical function

Explaination...

s=s+5 is an expression.

s=s+5; is a statement

In above example  the statement

double a; means a is the variable of double type.(represent a floating point(fractional))

The statement

b=Math.sqrt(a);  invokes the method sqrt avaiable in Math class.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @