Friday 18 January 2013

CLass X -Math Functions

The following are the built-in Mathematical functions:
1. Math.sqrt() - finds the square root
2. Math.pow() - finds the power raised to the specified base
3. Mth.min() - finds the smallest of the two numbers
4. Math.max()- finds the biggest of the two numbers
5. Math.abs() - It returns an absolute value.
    Math.abs(5) = 5
    Math.abs(-125) =125
6. Math.round() - If the fractional part is below 0.5 then it returns the same integer otherwise it returns the next integer value.
         System.out.println(Math.round(6.55));   ans is 7
       System.out.println(Math.round(-12.55));  ans is -13

7. Math.floor() - returns the number down to the nearest  integer.
            System.out.println(Math.floor(3.2));              3.0
         System.out.println(Math.floor(3.98));            3.0
         System.out.println(Math.floor(-3.2));           -4.0

         System.out.println(Math.round(-3.98));    -4.0

8. Math.ceil() - Gives next higher integer
       System.out.println(Math.ceil(3.2));                4.0
       System.out.println(Math.ceil(3.98));              4.0
       System.out.println(Math.ceil(-3.2));             -3.0
       System.out.println(Math.ceil(-3.98));          -3.0

9. Math.exp() - gives the exponential value
10. Math.rint() - returns the truncated value of the number after removing the fractional part.
    System.out.println(Math.rint(3.8));                  3.0
11. Math.random() - returns a random number between 0 and 1. 
   To  get the integer random number between 1 and n .
  System.out.println((int)(Math.random()*n));

   NOTE :         

  •  Mathematical functions will give tha answer in double data type.So if you are storing in some variable,make it double.
  • If you are writing the ouput remember to write decimal point and zero.

No comments:

Post a Comment