Hello friends how are you, today in this blog i will teach you what is math function, types of math function, how math functions work and many programs using math functions in a very simple way.
Let's start
Here we are going to learn all math functions one by one
- 1 sin()
- It return the sine of given value.
class Easy { public static void main(String[] args) { System.out.println(Math.sin(2)); } } /* ### OUTPUT ### 0.9092974268256817 */
- 2 cos()
- It return the cosine of given value.
class Easy { public static void main(String[] args) { System.out.println(Math.cos(2)); } } /* ### OUTPUT ### -0.4161468365471424 */
- 3 tan()
- It return the tangent of given value.
class Easy { public static void main(String[] args) { System.out.println(Math.tan(2)); } } /* ### OUTPUT ### -2.185039863261519 */
- 4 pow()
- It is used to calculate the value of the first argument raised to the power of the second argument.
- Syntax:pow(base,power)
class Easy { public static void main(String[] args) { System.out.println(Math.pow(2,3)); //pow(base,power) } } /* ### OUTPUT ### 8.0 */
- 5 sqrt()
- It is used to calculate the square root of given input.
class Easy { public static void main(String[] args) { System.out.println(Math.sqrt(9));//3.0 System.out.println(Math.sqrt(8));//2.8284271247461903 } } /* ### OUTPUT ### 3.0 2.8284271247461903 */
- 6 cbrt()
- It is used to calculate the cube root of given input.
class Easy { public static void main(String[] args) { System.out.println(Math.cbrt(27));//3.0 System.out.println(Math.cbrt(12));//2.2894284851066637 } } /* ### OUTPUT ### 3.0 2.2894284851066637 */
- 7 exp()
- It is used to calculate the exponential value of given input.
class Easy { public static void main(String[] args) { System.out.println(Math.exp(2));//7.38905609893065 System.out.println(Math.exp(0));//1.0 } } /* ### OUTPUT ### 7.38905609893065 1.0 */
- 8 log()
- Used to find natural logarithm of input value.
class Easy { public static void main(String[] args) { System.out.println(Math.log(2));//0.6931471805599453 } } /* ### OUTPUT ### 0.6931471805599453 */
- 9 log10()
- Used to find logarithm of input value to the base of 10.
class Easy { public static void main(String[] args) { System.out.println(Math.log10(2));//0.3010299956639812 } } /* ### OUTPUT ### 0.3010299956639812 */
- 10 max()
- Returns the maximum value from two number.
class Easy { public static void main(String[] args) { System.out.println(Math.max(1.8, 8.5)); } } /* ### OUTPUT ### 8.5 */
- 11 min()
- Returns the minimum value from two number.
class Easy { public static void main(String[] args) { System.out.println(Math.min(1.8, 8.5)); } } /* ### OUTPUT ### 1.8 */
- 12 abs()
- Returns the absolute value from two number.
- It always returns positive value.
class Easy { public static void main(String[] args) { System.out.println(Math.abs(-5));//5 System.out.println(Math.abs(5));//5 } } /* ### OUTPUT ### 5 5 */
- 13 floor()
- Returns the largest integer value which is less than or equal to input value.
class Easy { 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 } } /* ### OUTPUT ### 3.0 3.0 3.0 */
- 14 ceil()
- Returns the smallest integer value which is greater than or equal to input value.
class Easy { 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 } } /* ### OUTPUT ### 4.0 4.0 4.0 */
- 15 round()
- It returns the nearest integer value of the value(float,long etc) passed to this function.
class Easy { 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 } } /* ### OUTPUT ### 3 4 4 */
- 16 rint()
class Easy { 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 } } /* ### OUTPUT ### 3.0 4.0 4.0 */
- 17 random()
- It returns random value/number.
class Easy { public static void main(String[] args) { System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random()); } } /* ### OUTPUT ### 0.9128583349201873 0.5910358792968092 0.36135967543505065 */
- 18 toDegrees()
- It converts the given value into degrees.
class Easy { public static void main(String[] args) { System.out.println(Math.toDegrees(10)); } } /* ### OUTPUT ### 572.9577951308232 */
- 19 toRadians()
- It converts the given value into radians.
class Easy { public static void main(String[] args) { System.out.println("Radians of 10="+Math.toRadians(10)); } } /* ### OUTPUT ### Radians of 10=0.17453292519943295 */
- 20 compareTo()
- 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 Easy { 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 } } /* ### OUTPUT ### 1 0 -1 */
- 21 equals()
- 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 Easy { public static void main(String[] args) { Integer x=10; System.out.println(x.equals(5));//false System.out.println(x.equals(10));//true } } /* ### OUTPUT ### false true */
- 22 parseInt()
- It is used to convert string into integer.
class Easy { 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 } } /* ### OUTPUT ### 1010 20 20.0 20.0 */
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thanks.😊
0 Comments