Witshaper is the Professional (Computing Skills, English Learning and Soft Skills) Skill Development Training Center. We provide Professional IT Courses and Soft Skill Training in Dehradun to Students, Employees and organization. Who wish to pursue a career in IT Technology. Witshaper is led by a motivated team of IT experts and Soft Skill Professionals. We provide high quality trainings. Our Emphasis is on giving the practical knowledge to the students, so that they will get to know in depth and never forget what they opt, we provide to the students real learning environment. Witshaper prepares students and professionals to be the part of this growing industry. Be a part of Witshaper and get your dreams successful

Sunday 15 May 2016

Math class methods for finding power, absolute value, min, max and rounding number etc in java

import java.lang.Math;

public class MathDemo {

   public static void main(String[] args) {

   // get some doubles to find their absolute values
   double x = 4876.1874d;
 
   //syntax is static double abs(double a)
   // get and print their absolute values
   System.out.println("Math.abs(" + x + ")=" + Math.abs(x));//abs method returns the absolute value of the argument.
 
    // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc cosine of x
   System.out.println("Math.acos(" + x + ")=" + Math.acos(x));

 // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc sine of x
   System.out.println("Math.asin(" + x + ")=" + Math.asin(x));



   // get a variable x which is equal to PI/2
   double x = Math.PI / 2;

   // convert x to radians
   x = Math.toRadians(x);

   // get the arc tangent of x
   System.out.println("Math.atan(" + x + ")" + Math.atan(x));

// get two double numbers
   double x = 45.0;
   double y = 180.0;

   // convert them to radians
   x = Math.toRadians(x);
   y = Math.toRadians(y);

   // print their cosine
   System.out.println("Math.cos(" + x + ")=" + Math.cos(x));
   System.out.println("Math.cos(" + y + ")=" + Math.cos(y));

 // get two double numbers
   double x = 5;
   double y = 0.5;

   // print e raised at x and y
   System.out.println("Math.exp(" + x + ")=" + Math.exp(x));
   System.out.println("Math.exp(" + y + ")=" + Math.exp(y));

// get two double numbers
   double x = 60984.1;
   double y = -497.99;

   // call floor and print the result
   System.out.println("Math.floor(" + x + ")=" + Math.floor(x));
   System.out.println("Math.floor(" + y + ")=" + Math.floor(y));
   System.out.println("Math.floor(0)=" + Math.floor(0));


   // get two double numbers
   double x = 60984.1;
   double y = -497.99;

   // get the natural logarithm for x
   System.out.println("Math.log(" + x + ")=" + Math.log(x));

   // get the natural logarithm for y
   System.out.println("Math.log(" + y + ")=" + Math.log(y));


   // get two integer numbers
   int x = 60984;
   int y = 1000;

   // print the larger number between x and y
   System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y));

 // get two random double numbers
   double x = Math.random();
   double y = Math.random();

   // print the numbers and print the higher one
   System.out.println("Random number 1:" + x);
   System.out.println("Random number 2:" + y);
   System.out.println("Highest number:" + Math.max(x, y));
   }
}



2 .program  

/*
 * Program to demonstrate the use of Math class methods
 */

import java.io.*;
public class Mathstest {


public static void main(String[] args)
{
int num1,num2,x,y,z;
double l1,l2,l3;
String str;
BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter a number");
str=bf.readLine();
num1=Integer.parseInt(str);
x=Math.abs(num1);
System.out.println("Absolute value of "+num1+" is ="+x);

System.out.println("Enter first number");
str=bf.readLine();
num1=Integer.parseInt(str);
System.out.println("Enter second number");
str=bf.readLine();

num2=Integer.parseInt(str);
y=Math.max(num1, num2);
System.out.println("Largest number is ="+y);
y=Math.min(num1, num2);
System.out.println("Smallest number is ="+y);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.log(l1);
System.out.println("Log of "+l1+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.sqrt(l1);
System.out.println("Square root of "+l1+" is ="+l2);
l3=Math.floor(l2);
System.out.println("Floor value of "+Math.sqrt(l1)+" is ="+l3);
l2=Math.ceil(l2);
System.out.println("Ceil value of "+Math.sqrt(l1)+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
l2=Math.cbrt(l1);
System.out.println("Cube root of "+l1+" is ="+l2);
System.out.println("Enter a decimal number");
str=bf.readLine();

l1=Float.valueOf(str);
System.out.println("Enter the power to raise ");
str=bf.readLine();

l2=Float.valueOf(str);
l3=Math.pow(l1, l2);
System.out.println(l1+"raised to the power "+l2+ " = "+l3);
}
catch(Exception e){}


}


}

1 comment:

  1. I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting java online training

    ReplyDelete