Mathematical Example and Exercises

Compiled By Unknown - No Comments
Absolute value. Let us start. An absolute value is the number with no negative sign. If the number is already positive, no change is made. We first import java.lang.Math.

Floor The floor is always beneath us.

Ceiling. With this method, a number is always increased if it has a fractional part. It does not matter how close to the lower number the number is—it is increased by ceil().
Square root. Consider this program—it does not import java.lang.Math at its top. Instead we directly access Math.sqrt using its composite name.
Java Program on Math.E, PI. Sometimes programs need to use the E and pi constants. With the Math class, we can access these as fields. We do not need to specify 3.14 for pi.
Exact methods. The Math class provides "exact" methods. These do what we expect (add, subtract, multiply) but throw an ArithmeticException when the result overflows.
So: In a computation where overflow would be disastrous, these methods help check that the program is correct.
Pow stands for power. If you have ever been in a math course, you already know what Math.pow does. It raises the first number to the power of the second number.
Compound interest: We use Math.pow() to compute compound interest, which is an exponential function.


For More Examples and Exercises for Java Programming Visit
Java Program to Calculate Area of Circle
Java Program Example to calculate Perimeter Of Circle
Java Program Example to Calculate Area of Rectangle
Java Program Example to Calculate perimeter of Rectangle

Java program Example to show even or odd number
Java Program Example to find Largest and Smallest Number
Java Program Example on Factorial
Java Program example on Factorial Using Recursion
Java Program Example on Reverse Number
Java Program Example on Swap Numbers
Java Program Example on Swap Numbers Without Using Third Variable







3. maximum value using java Math Class

Syntax:-

Math.max(variable1,variable2)

Here variable can be double,float,int and long datatype

max method is used to find maximum value between variable1 and variable2.

Math.max(2,8) = 8

4.Rounding number using java Math Class

Syntax:-

Math.ceil(variable)

Math.floor(variable)

Here variable is double datatype

ceil method is used round up the variable and floor method is used round down the variable.

Math.ceil(8.4) = 9

Math.floor(8.4) = 8

5. power of the number using java Math Class

Syntax:-

Math.pow(variable1,variable2)

Here variable is double datatype

here variable1 is base value and variable2 is power value.


Math.pow(2,3) = 8

7. cube root of the number using java Math Class

Syntax:-

Math.cbrt(variable)

Here variable is double datatype

cbrt method is used for finding cube root of a number.

Math.cbrt(27) = 3

8. copy sign of the number using java Math Class

Syntax:-

Math.copySign(variable1, variable2)

Here variable is float and double datatype

copySign method is used to copy sign of variable2 and assign that sign to variable1.

Math.copySign(2,-8.3) = -2.0

Math.copySign(2,8.3) = 2.0

9. Euler's number e to power of a number using java Math Class

Syntax:-

Math.exp(variable)

Here variable is double datatype

Euler's number e raised to the power of a variable.

Math.exp(2) = 7.38905609893065

10. Euler's number e to power of a number minus 1 using java Math Class

Syntax:-

Math.expm1(variable)

Here variable is double datatype

Euler's number e raised to the power of a variable minus 1 ie e^(variable)-1

Math.expm1(2) = 6.38905609893065


 public class MathLibraryExample {  
  public static void main(String[] args) {  
   int i = 7;  
   int j = -9;  
   double x = 72.3;  
   double y = 0.34;  
   System.out.println("i is " + i);     
   System.out.println("j is " + j);  
   System.out.println("x is " + x);     
   System.out.println("y is " + y);  
   // The absolute value of a number is equal to   
   // the number if the number is positive or   
   // zero and equal to the negative of the number   
   // if the number is negative.  
   System.out.println("|" + i + "| is " + Math.abs(i));     
   System.out.println("|" + j + "| is " + Math.abs(j));  
   System.out.println("|" + x + "| is " + Math.abs(x));     
   System.out.println("|" + y + "| is " + Math.abs(y));  
   // Truncating and Rounding functions  
   // You can round off a floating point number   
   // to the nearest integer with round()  
    System.out.println(x + " is approximately " + Math.round(x));     
    System.out.println(y + " is approximately " + Math.round(y));     
   // The "ceiling" of a number is the    
   // smallest integer greater than or equal to  
   // the number. Every integer is its own   
   // ceiling.  
    System.out.println("The ceiling of " + i + " is " + Math.ceil(i));     
    System.out.println("The ceiling of " + j + " is " + Math.ceil(j));  
    System.out.println("The ceiling of " + x + " is " + Math.ceil(x));     
    System.out.println("The ceiling of " + y + " is " + Math.ceil(y));  
    // The "floor" of a number is the largest   
    // integer less than or equal to the number.  
    // Every integer is its own floor.  
    System.out.println("The floor of " + i + " is " + Math.floor(i));     
    System.out.println("The floor of " + j + " is " + Math.floor(j));  
    System.out.println("The floor of " + x + " is " + Math.floor(x));     
    System.out.println("The floor of " + y + " is " + Math.floor(y));  
    // Comparison operators  
    // min() returns the smaller of the two arguments you pass it  
    System.out.println("min(" + i + "," + j + ") is " + Math.min(i,j));     
    System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y));     
    System.out.println("min(" + i + "," + x + ") is " + Math.min(i,x));     
    System.out.println("min(" + y + "," + j + ") is " + Math.min(y,j));     
    // There's a corresponding max() method   
    // that returns the larger of two numbers   
    System.out.println("max(" + i + "," + j + ") is " + Math.max(i,j));     
    System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y));     
    System.out.println("max(" + i + "," + x + ") is " + Math.max(i,x));     
    System.out.println("max(" + y + "," + j + ") is " + Math.max(y,j));     
    // The Math library defines a couple   
    // of useful constants:  
    System.out.println("Pi is " + Math.PI);     
    System.out.println("e is " + Math.E);      
    // Trigonometric methods  
   // All arguments are given in radians  
   // Convert a 45 degree angle to radians  
   double angle = 45.0 * 2.0 * Math.PI/360.0;  
   System.out.println("cos(" + angle + ") is " + Math.cos(angle));     
   System.out.println("sin(" + angle + ") is " + Math.sin(angle));     
    // Inverse Trigonometric methods  
    // All values are returned as radians  
   double value = 0.707;  
   System.out.println("acos(" + value + ") is " + Math.acos(value));     
   System.out.println("asin(" + value + ") is " + Math.asin(value));     
   System.out.println("atan(" + value + ") is " + Math.atan(value));     
   // Exponential and Logarithmic Methods  
   // exp(a) returns e (2.71828...) raised   
   // to the power of a.    
   System.out.println("exp(1.0) is " + Math.exp(1.0));  
   System.out.println("exp(10.0) is " + Math.exp(10.0));  
   System.out.println("exp(0.0) is " + Math.exp(0.0));  
   // log(a) returns the natural   
   // logarithm (base e) of a.   
   System.out.println("log(1.0) is "  + Math.log(1.0));  
   System.out.println("log(10.0) is "  + Math.log(10.0));  
   System.out.println("log(Math.E) is " + Math.log(Math.E));  
   // pow(x, y) returns the x raised   
   // to the yth power.  
   System.out.println("pow(2.0, 2.0) is " + Math.pow(2.0,2.0));  
   System.out.println("pow(10.0, 3.5) is " + Math.pow(10.0,3.5));  
   System.out.println("pow(8, -1) is "   + Math.pow(8,-1));  
   // sqrt(x) returns the square root of x.  
   for (i=0; i < 10; i++) {  
    System.out.println(  
     "The square root of " + i + " is " + Math.sqrt(i));  
   }  
   // Finally there's one Random method   
   // that returns a pseudo-random number   
   // between 0.0 and 1.0;  
   System.out.println("Here's one random number: " + Math.random());     
   System.out.println("Here's another random number: " + Math.random());  
  }  
 }  







Tags:

No Comment to " Mathematical Example and Exercises "