Skip to main content

Chapter- 16 What is Math Function in Java

 

 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

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...