0% found this document useful (0 votes)
3 views15 pages

Unit 6 Math Functions

The document provides an overview of various Java mathematical functions, including Math.min(), Math.max(), Math.round(), Math.sqrt(), Math.cbrt(), Math.pow(), Math.ceil(), Math.floor(), Math.random(), and Math.abs(), along with their descriptions, syntax, and examples. It also includes sample Java programs demonstrating the use of these functions for tasks such as finding the greatest and smallest of three numbers, calculating the hypotenuse of a triangle, and evaluating various mathematical operations based on user input. Additionally, it covers the differences between related functions like Math.rint() and Math.round().

Uploaded by

sheela
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Unit 6 Math Functions

The document provides an overview of various Java mathematical functions, including Math.min(), Math.max(), Math.round(), Math.sqrt(), Math.cbrt(), Math.pow(), Math.ceil(), Math.floor(), Math.random(), and Math.abs(), along with their descriptions, syntax, and examples. It also includes sample Java programs demonstrating the use of these functions for tasks such as finding the greatest and smallest of three numbers, calculating the hypotenuse of a triangle, and evaluating various mathematical operations based on user input. Additionally, it covers the differences between related functions like Math.rint() and Math.round().

Uploaded by

sheela
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA math Function

[Link]
java_mathematical_functions

Basic Method with Description


1) [Link]()
● This function returns the Smallest of two values.
● Return value depends on the values used in the arguments.
● One int and one double argument will give the answer in double.
● Eg: int small =[Link](5,9)
● Answer is 5
2)[Link]()
● This function returns the biggest of two values.
● Return value depends on the values used in the arguments.
● One int and one double argument will give the answer in double.
● Eg: int small =[Link](5,9)
● Answer is 9

3) [Link]()
● This function returns the value in the rounded form of the decimal
value given.
● a) if the functional part is less than 0.5 it will return the same
value of the integer part of the argument .
● b) if the functional part is greater than or equal to 0.5 it will return
the next higher value of the integer part of the argument .
● Return type is always int or long.
● Give different outputs for positive and negative numbers.
Syntax: <return type> variable =[Link](number);
Example : int a =[Link](8.3);

Positive Output
numbers
8
[Link](8.0)
8
[Link](8.49)
9
[Link](8.5)
9
[Link](8.99)

Negative Output
numbers
-8
[Link](-8.0)
-8
[Link](-8.5)
-9
[Link](-8.51)
-9
[Link](-8.99)

4) [Link]()
● It is used to return the square root of a positive number as a
double type value.
Syntax:
<return type> variable =[Link](positive number);
Example:
output
Function
2.0
Double n= [Link](4.0);
2.5
Double n= [Link](6.25);
NaN
Double n= [Link](-3.25); (Not a number)
Square root of an imaginary number is an imaginary number. Hence it
gives an answer as NaN.

5) [Link]()
It is used to return the cube root of a positive or negative number.
Return type is double.
Syntax:
<return type> variable =[Link](positive number/ negative number);

Output
Function
3.0
Double a= [Link](27.0);

2.5
Double a=
[Link](15.625));
-1.5
Double a= [Link](-3.35);

6) [Link]()
● It returns the value of the first argument raised to the power of the
second argument.
● Return type is double.
● A power b is read as “a raised to the power b”
Here ,
a-> base
b->index
Syntax:
<Return type> variable = [Link](a,b);
Example:
Int ans=[Link](2,3);
The answer is 8.0

7) [Link]()
● It is used to find the highest double value that is greater than or
equal to the argument .
● Returns a double value.
Syntax:
<return type>variable = [Link](argument);

Positive numbers Output


[Link](8) 8.0
[Link](8.5) 9.0
[Link](8.91) 9.0
Negative numbers Output
[Link](-0.45) -0.0
[Link](-8) -8.0
[Link](-8.5)) -8.0
[Link](-8.912) -8.0
8) [Link]()
● Returns the lower double for the given argument as double data
type .
● The argument can be an int, float,double.
● Return type is double
Syntax:
<return type> variable = [Link](argument);

Positive number Output


[Link](8) 8.0
[Link](8.912) 8.0
Negative number Output
[Link](-4.8) -5.0
[Link](-8) -8.0
[Link](-8.91) -9.0

9) [Link]()
● It returns a double value with a positive sign, greater than or equal
to 0.0 and less than 1.0.
Syntax:
<return type> variable =[Link]();
Example:
Double d = [Link]();
Note :
a) To get an integer between 0 and n,
● Int r=(int)([Link]()*n)+1;
b) To get an integer between m(lower limit) and n(upper limit)
● Int r=(int)(([Link]()*(n-m))+m);
c)To get head(0)/tails(1)
● Int n=(int)([Link]()*2);

12)[Link]()
● Returns the absolute value for the given argument .
● It returns int/long /double depending upon the arguments supplied.

Syntax:

<return type> variable = [Link](argument);

Example :
Double m = [Link](-1.23);
Ans will be 1.23 and it will be stored in the variable m

[Link]( ) [Link]( )

Rounds off its argument to the It rounds off its argument to the nearest
nearest mathematical integer mathematical integer and returns its value as an
and returns its value as a int or long type.
double type.
● If argument is float, return type is int,
● [Link](4.4)=4.0 ● if argument is double, return type is long.
● [Link](5.6)=6.0

● [Link](4.4)=4
● [Link](5.6)=6

Note: They (rint and round) give same answers


for non- midpoint arguments.

At the mid-point, it returns the At the mid-point, it returns the higher integer.
integer that is even.
● [Link](4.5)=5
● [Link](4.5)=4.0 ● [Link](5.5)=6
● [Link](5.5)=6.0

[Link] [Link]
Takes float as input parameter and Takes the floating value as an input
returns an integer as an output parameter and returns an integer as
parameter. an output parameter.

Returns the integer value just greater Returns the integer value just lesser
than the given rational value. than the given rational value.

[Link](78.38) is 79 [Link](78.38) is 78
[Link] (-39.78 )is -39. [Link](-39.78) is -40

[Link] [Link]

Takes float as input parameter and


● Takes int, float as
returns an integer as an output
parameter.
parameter.
● Returns a double value.
Returns the integer value just greater
than the given rational value. ● Returns the highest
integer value that is
greater than or equal to
the argument .
[Link](78.38) is 79
[Link] (-39.78 )is -39. [Link](78.38 ) is 79.0
[Link] (-39.78 )is -38.0

Question 1

Write a program in Java to input three numbers and display the greatest and the smallest of the
two numbers.
Hint: Use [Link]( ) and [Link]( )
Sample Input: 87, 65, 34
Sample Output: Greatest Number 87
Smallest number 34

import [Link];

public class KboatGreatestNumber

public static void main(String args[]) {


Scanner in = new Scanner([Link]);

[Link]("Enter First Number: ");

int a = [Link]();

[Link]("Enter Second Number: ");

int b = [Link]();

[Link]("Enter Third Number: ");

int c = [Link]();

int g = [Link](a, b);

g = [Link](g, c);

int s = [Link](a, b);

s = [Link](s, c);

[Link]("Greatest Number = " + g);

[Link]("Smallest Number = " + s);

Output

Question 2

Write a program in Java to calculate and display the hypotenuse of a Right-Angled Triangle by
taking perpendicular and base as inputs.
Hint: h = √p2 + b2

import [Link];

public class KboatHypotenuse

public static void main(String args[]) {


Scanner in = new Scanner([Link]);

[Link]("Enter Perpendicular: ");

double p = [Link]();

[Link]("Enter Base: ");

double b = [Link]();

double h = [Link]([Link](p, 2) + [Link](b, 2));

[Link]("Hypotenuse = " + h);

Output

Question 3

Write a program to input a number and evaluate the results based on the number entered by the
user:
(a) Natural logarithm of the number
(b) Absolute value of the number
(c) Square root of the number
(d) Cube of the number
(e) Random numbers between 0 (zero) and 1 (one).

import [Link];

public class KboatMathMethods

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter Number: ");

double n = [Link]();

[Link]("Natural logarithm = " + [Link](n));

[Link]("Absolute value = " + [Link](n));


[Link]("Square root = " + [Link](n));

[Link]("Cube root= " + [Link](n));

[Link]("Random number = " + [Link]());

Output

Question 4

In an examination, you have appeared for three subjects i.e. Physics, Chemistry and Biology.
Write a program in Java to calculate the average mark obtained and finally display the marks in
rounded-off form.
Take Physics, Chemistry. and Biology marks as inputs.

import [Link];

public class KboatAvgMarks

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter Marks");

[Link]("Physics: ");

int p = [Link]();

[Link]("Chemistry: ");

int c = [Link]();

[Link]("Biology: ");

int b = [Link]();

double avg = (p + c + b) / 3.0;

long roundAvg = [Link](avg);

[Link]("Rounded Off Avg Marks = " + roundAvg);

}
}

Output

Question 5

You want to calculate the radius of a circle by using the formula:


Area = (22/7) * r2; where r = radius of a circle
Hence the radius can be calculated as:
r = √((7 * area) / 22)
Write a program in Java to calculate and display the radius of a circle by taking
area as an input.

import [Link];

public class KboatCircleRadius

public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter Area of Circle: ");

double area = [Link]();

double r = [Link](7 * area / 22);

[Link]("Radius of Circle = " + r);

public class MathLibraryExample {

public static void main(String[] args) {

int i = 7;

int j = -9;

double x = 72.3;

double y = 0.34;

[Link]("i is " + i);


[Link]("j is " + j);

[Link]("x is " + x);

[Link]("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.

[Link]("|" + i + "| is " + [Link](i));

[Link]("|" + j + "| is " + [Link](j));

[Link]("|" + x + "| is " + [Link](x));

[Link]("|" + y + "| is " + [Link](y));

// Truncating and Rounding functions

// You can round off a floating point number to the nearest integer with round()

[Link](x + " is approximately " + [Link](x));

[Link](y + " is approximately " + [Link](y));

// The "ceiling" of a number is the smallest integer greater than or equal to the number.
Every integer is its own //ceiling.

[Link]("The ceiling of " + i + " is " + [Link](i));

[Link]("The ceiling of " + j + " is " + [Link](j));

[Link]("The ceiling of " + x + " is " + [Link](x));

[Link]("The ceiling of " + y + " is " + [Link](y));

// The "floor" of a number is the largest integer less than or equal to the number.

// Every integer is its own floor.

[Link]("The floor of " + i + " is " + [Link](i));

[Link]("The floor of " + j + " is " + [Link](j));

[Link]("The floor of " + x + " is " + [Link](x));

[Link]("The floor of " + y + " is " + [Link](y));


// Comparison operators

// min() returns the smaller of the two arguments you pass it

[Link]("min(" + i + "," + j + ") is " + [Link](i,j));

[Link]("min(" + x + "," + y + ") is " + [Link](x,y));

[Link]("min(" + i + "," + x + ") is " + [Link](i,x));

[Link]("min(" + y + "," + j + ") is " + [Link](y,j));

// There's a corresponding max() method

// that returns the larger of two numbers

[Link]("max(" + i + "," + j + ") is " + [Link](i,j));

[Link]("max(" + x + "," + y + ") is " + [Link](x,y));

[Link]("max(" + i + "," + x + ") is " + [Link](i,x));

[Link]("max(" + y + "," + j + ") is " + [Link](y,j));

// The Math library defines a couple of useful constants:

[Link]("Pi is " + [Link]);

[Link]("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 * [Link]/360.0;

[Link]("cos(" + angle + ") is " + [Link](angle));

[Link]("sin(" + angle + ") is " + [Link](angle));

// Inverse Trigonometric methods. All values are returned as radians

double value = 0.707;


[Link]("acos(" + value + ") is " + [Link](value));

[Link]("asin(" + value + ") is " + [Link](value));

[Link]("atan(" + value + ") is " + [Link](value));

// Exponential and Logarithmic Methods

// exp(a) returns e (2.71828...) raised

// to the power of a.

[Link]("exp(1.0) is " + [Link](1.0));

[Link]("exp(10.0) is " + [Link](10.0));

[Link]("exp(0.0) is " + [Link](0.0));

// log(a) returns the natural

// logarithm (base e) of a.

[Link]("log(1.0) is " + [Link](1.0));

[Link]("log(10.0) is " + [Link](10.0));

[Link]("log(Math.E) is " + [Link](Math.E));

// pow(x, y) returns the x raised

// to the yth power.

[Link]("pow(2.0, 2.0) is " + [Link](2.0,2.0));

[Link]("pow(10.0, 3.5) is " + [Link](10.0,3.5));

[Link]("pow(8, -1) is " + [Link](8,-1));

// sqrt(x) returns the square root of x.

for (i=0; i < 10; i++) {


[Link](

"The square root of " + i + " is " + [Link](i));

// Finally there's one Random method

// that returns a pseudo-random number

// between 0.0 and 1.0;

[Link]("Here's one random number: " + [Link]());

[Link]("Here's another random number: " + [Link]());

You might also like