0% found this document useful (0 votes)
90 views20 pages

Public Class Findabsolutevalueexample (

To find absolute value of int, use static int abs(int i) method. It returns the same value if the agrument is non negative value, otherwise negation of the negative value.

Uploaded by

Baljeet Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views20 pages

Public Class Findabsolutevalueexample (

To find absolute value of int, use static int abs(int i) method. It returns the same value if the agrument is non negative value, otherwise negation of the negative value.

Uploaded by

Baljeet Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

public class FindAbsoluteValueExample {

public static void main(String[] args) {

int i = 8;

int j = -5;

/*

  * To find absolute value of int, use

  * static int abs(int i) method.

  *

  * It returns the same value if the agrument is non negative value, otherwise

  * negation of the negative value.

  *

  */

System.out.println("Absolute value of " + i + " is :" + Math.abs(i));

System.out.println("Absolute value of " + j + " is :" + Math.abs(j));

float f1 = 10.40f;

float f2 = -50.28f;

/*

  * To find absolute value of float, use

  * static float abs(float f) method.

  *
  * It returns the same value if the agrument is non negative value, otherwise

  * negation of the negative value.

  *

  */

System.out.println("Absolute value of " + f1 + " is :" + Math.abs(f1));

System.out.println("Absolute value of " + f2 + " is :" + Math.abs(f2));

double d1 = 43.324;

double d2 = -349.324;

/*

  * To find absolute value of double, use

  * static double abs(double d) method.

  *

  * It returns the same value if the agrument is non negative value, otherwise

  * negation of the negative value.

  *

  */

System.out.println("Absolute value of " + d1 + " is :" + Math.abs(d1));

System.out.println("Absolute value of " + d2 + " is :" + Math.abs(d2));

long l1 = 34;

long l2 = -439;

/*

  * To find absolute value of long, use

  * static long abs(long l) method.

  *

  * It returns the same value if the agrument is non negative value, otherwise
  * negation of the negative value.

  *

  */

System.out.println("Absolute value of " + l1 + " is :" + Math.abs(l1));

System.out.println("Absolute value of " + l2 + " is :" + Math.abs(l2));

/*

Output would be

Absolute value of 8 is :8

Absolute value of -5 is :5

Absolute value of 10.4 is :10.4

Absolute value of -50.28 is :50.28

Absolute value of 43.324 is :43.324

Absolute value of -349.324 is :349.324

Absolute value of 34 is :34

Absolute value of -439 is :439

*/
public class FindCeilingExample {

public static void main(String[] args) {

/*

  * To find a ceiling value, use

  * static double ceil(double d) method of Math class.

  *

  * It returns a smallest integer which is not less than the argument value.

  */

//Returns the same value

System.out.println(Math.ceil(10));

//returns a smallest integer which is not less than 10.1, i.e. 11

System.out.println(Math.ceil(10.1));

//returns a smallest integer which is not less than 5.5, i.e. 6

System.out.println(Math.ceil(5.5));

//in this case it would be -20

System.out.println(Math.ceil(-20));

//it returns -42 not -43. (-42 is grater than 42.4 :) )

System.out.println(Math.ceil(-42.4));

//returns 0
System.out.println(Math.ceil(0));

/*

Output would be

10.0

11.0

6.0

-20.0

-42.0

0.0

*/
public class FindExponentialNumberExample {

public static void main(String[] args) {

/*

  * To find exponential value of a number, use

  * static double exp(double d) method of Java Math class.

  *

  * It returns e raised to argument value.

  */

System.out.println("Exponential of 2 is : " + Math.exp(2));

/*

Output would be

Exponential of 2 is : 7.38905609893065

*/
public class FindFloorValueExample {

public static void main(String[] args) {

/*

  * To find a floor value, use

  * static double floor(double d) method of Math class.

  *

  * It returns a largest integer which is not grater than the argument value.

  */

//Returns the same value

System.out.println(Math.floor(70));

//returns largest integer which is not less than 30.1, i.e. 30

System.out.println(Math.floor(30.1));

//returns largest integer which is not less than 15.5, i.e. 15

System.out.println(Math.floor(15.5));

//in this case it would be -40

System.out.println(Math.floor(-40));

//it returns -43.

System.out.println(Math.floor(-42.4));

//returns 0
System.out.println(Math.floor(0));

/*

Output would be

70.0

30.0

15.0

-40.0

-43.0

0.0

*/
public class FindMaxOfTwoNumbersExample {

public static void main(String[] args) {

/*

  * To find maximum of two int values, use

  * static int max(int a, int b) method of Math class.

  */

System.out.println(Math.max(20,40));

/*

  * To find minimum of two float values, use

  * static float max(float f1, float f2) method of Math class.

  */

System.out.println(Math.max(324.34f,432.324f));

/*

  * To find maximum of two double values, use

  * static double max(double d2, double d2) method of Math class.

  */

System.out.println(Math.max(65.34,123.45));

/*

  * To find maximum of two long values, use

  * static long max(long l1, long l2) method of Math class.

  */
 

System.out.println(Math.max(435l,523l));

/*

Output would be

40

432.324

123.45

523
public class FindMinimumOfTwoNumbersExample {

public static void main(String[] args) {

/*

  * To find minimum of two int values, use

  * static int min(int a, int b) method of Math class.

  */

System.out.println(Math.min(34,45));

/*

  * To find minimum of two float values, use

  * static float min(float f1, float f2) method of Math class.

  */

System.out.println(Math.min(43.34f, 23.34f));

/*

  * To find minimum of two double values, use

  * static double min(double d2, double d2) method of Math class.

  */

System.out.println(Math.min(4324.334, 3987.342));

/*

  * To find minimum of two long values, use

  * static long min(long l1, long l2) method of Math class.

  */
 

System.out.println(Math.min(48092840,4230843));

/*

Output would be

34

23.34

3987.342

4230843

*/
public class FindNaturalLogarithmOfNumberExample {

public static void main(String[] args) {

/*

  * To find natural logarithm value of a number, use

  * static double log(double d) method of Java Math class.

  */

System.out.println("Natural logarithm value of 2 is : " + Math.log(2));

/*

Output would be

Natural logarithm value of 2 is : 0.6931471805599453


public class FindPowerExample {

public static void main(String[] args) {

/*

  * To find a value raised to power of another value, use

  * static double pow(double d1, double d2) method of Java Math class.

  */

//returns 2 raised to 2, i.e. 4

System.out.println(Math.pow(2,2));

//returns -3 raised to 2, i.e. 9

System.out.println(Math.pow(-3,2));

/*

Output would be

4.0

9.0

*/
public class FindSquareRootExample {

public static void main(String[] args) {

/*

  * To find square root value of a number, use

  * static double sqrt(double d1) method Java Math class.

  */

//returns square root of 9, i.e. 3

System.out.println(Math.sqrt(9));

//returns square root of 25.5

System.out.println(Math.sqrt(25.5));

/*

Output would be

3.0

5.049752469181039

*/
public class GenerateRandomIntByRange {

public static void main(String args[]){

/*

The "(int)" parses the double to an int value replace 10 with

your range of number.

If you want a number between 5 and 15 the range is 10 [15-5]

replace the 5 with the staring number.

If you want the lowest number in the range to be 5 then add 5.

Example :

int ran = (int)(Math.random()*100)-50;

will return a value in the range [-50;50]

*/

int random = (int)(Math.random()* 10 ) + 5;

System.out.println(random);

}
public class GenerateRandomNumbers {

public static void main(String[] args) {

/*

  * To generate random numbers, use

  * static double random() method of Java Math class.

  *

  * This method returns a positive double value grater than 0.0

  * and less than 1.0

  */

System.out.println("Random numbers between 0.0 and 1.0 are,");

for(int i=0; i < 5 ; i++)

System.out.println("Random Number ["+ (i+1) + "] : " + Math.random());

/*

  * To generate random number between 1 to 100 use following code

  */

System.out.println("Random numbers between 1 and 100 are,");

for(int i=0; i < 5 ; i++)

System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*100));

 
/*

Typical output would be

Random numbers between 0.0 and 1.0 are,

Random Number [1] : 0.7900395454653136

Random Number [2] : 0.15887365598103076

Random Number [3] : 0.5570570713930629

Random Number [4] : 0.017811004461356195

Random Number [5] : 0.7135560403213513

Random numbers between 1 and 100 are,

Random Number [1] : 31

Random Number [2] : 21

Random Number [3] : 24

Random Number [4] : 95

Random Number [5] : 3

*/
public class RounFloatDoubleNumbersExample {

public static void main(String[] args) {

/*

  * To round float number, use

  * static int round(float f) method of Java Math class.

  *

  * It returns closest int number to the argument.

  * Internally, it adds 0.5 to the argument, takes floor value and casts

  * the result into int.

  *

  * i.e. result = (int) Math.floor( argument value + 0.5f )

  */

//returns same value

System.out.println(Math.round(10f));

// returns (int) Math.floor(10.6) = 10

System.out.println(Math.round(20.5f));

//returns (int) Math.floor(20.5 + 0.5) = 30

System.out.println(Math.round(20.5f));

//returns (int) Math.floor(-18.9) = 19

System.out.println(Math.round(-19.4f));

 
//returns (int) Math.floor(-23) = -23

System.out.println(Math.round(-23.5f));

/*

  * To round double numbers, use

  * static long round(double d) method of Java Math class.

  * It returns long.

  */

/*

Output would be

10

21

21

-19

-23

*/

You might also like