[go: up one dir, main page]

0% found this document useful (0 votes)
53 views18 pages

Mathematical Methods

The document provides an overview of mathematical methods available in Java's Math class, including functions like abs(), sqrt(), pow(), cbrt(), max(), min(), log(), round(), rint(), ceil(), floor(), and random(). Each function is explained with its syntax, return type, and examples of usage. Additionally, it includes sample programs that demonstrate how to use these methods for various calculations.

Uploaded by

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

Mathematical Methods

The document provides an overview of mathematical methods available in Java's Math class, including functions like abs(), sqrt(), pow(), cbrt(), max(), min(), log(), round(), rint(), ceil(), floor(), and random(). Each function is explained with its syntax, return type, and examples of usage. Additionally, it includes sample programs that demonstrate how to use these methods for various calculations.

Uploaded by

bosekunal.cyber
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Mathematical Methods

Mathematical Methods in Java are present in java.lang package.


To use mathematical functions we do not require to import java.lang package, as it is a
default package of java (i.e. the package which is already imported)
All mathematical functions are present in the Math class.
i) Math.abs(): This function accepts an argument and return the absolute value of the
given argument (i.e. positive value of the argument). Return type is number (int, long,
float, double) and it depends upon the type of argument passed.
Syntax: Math.abs(argument);
Eg:
int a = Math.abs(-12);
= 12
float f = Math.abs(-2.3f);
= 2.3
int d = Math.abs(13);
= 13
ii) Math.sqrt(): This function accepts an argument and return the square root of the
given argument. Return data type of this function is double.
Syntax: <double> variable = Math.sqrt(argument);
Eg:
double x = Math.sqrt(4);
= 2.0
double y = Math.sqrt(16.0);
= 4.0
Math.sqrt(-4.0); →NaN
NOTE: Square root of negative number cann’t be determined. When we find
square root of negative number we get the output as NaN.
iii) Math.pow(): This function takes two arguments. First argument is base and second
argument is exponent. It is used for finding power raised to the base. Return data type
is double.
Syntax: <double> variable = Math.pow(arg1, arg2);
Eg:
double d = Math.pow(2,4);
= 16.0
double f = Math.pow(4,3);
= 64.0
iv) Math.cbrt(): This function is used to find the cube root of the given argument.
Return data type is double.
Syntax: <double> variable = Math.cbrt(argument);
Eg:
double x = Math.cbrt(27);
= 3.0
Q. Write the java expression:

√𝑎2 + 𝑏2 = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));


Q. Write the return type of b and output of the given statement:
b = Math.sqrt(25);
Retrun type of b: double
Output: 5.0
Q. Write the return type for the following functions:
(i) Math.cbrt() (ii) Math.pow()
Ans:
(i) double
(ii) double
v) Math.max(): This function is used to find the largest number of the given two
arguments. Return type can be any numeric type, depending upon the type of input.
Syntax: Math.max(arg1, arg2);
Eg:
int a = Math.max(10,20);
= 20
double d = Math.max(1.2,1.5);
=1.5
double x = Math.max(2, 1.5);
= 2.0
When mixed type of data is given in the argument then type promotion first takes
place, hence in the above example 2 will be first converted to 2.0 and then max()
function will make the comparison. So the output is 2.0.
Eg:
Math.sqrt(Math.max(Math.abs(-3),9)); →
= Math.sqrt(Math.max(3, 9))
= Math.sqrt(9)
= 3.0
How to find maximum of three numbers using max() function.
Eg: int a = 10, b = 12, c = 5;
Ans1:
int d = Math.max(a,b);
int m = Math.max(d,c);
Ans2:
int m = Math.max(Math.max(a,b), c);
vi) Math.min(): This function is used to find the smallest number of the given two
arguments. Return type can be any numeric type, depending upon the type of input.
Syntax: Math.min(arg1, arg2);
Eg:
int a = Math.min(10,4);
=4
double k = Math.min(25.2,34.6);
= 25.2
Programs:
Q1) Write a program to calculate the result of the given expression: Display the result

√𝑎 2 + 𝑏 2 + 𝑐 2
Ans:
import java.util.*;
public class Expression
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.println("Enter the value for a, b and c");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
double result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2)+Math.pow(c,2));
System.out.println("Result of the expression = "+result);
}
}
Argument: Arguments are those which are passed in the parenthesis of function. They
can be any expression, variable, literals.
Eg:
Math.sqrt(2); Math.sqrt(x); Math.sqrt(a+b)
Programs:
Q1) Write a program to accept three sides of a triangle in a, b and c. Calculate and
display the area of triangle by the given formula.

Area = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐 )


𝑎+𝑏+𝑐
S=
2

Ans:
import java.util.*;
public class AreaOfTriangle
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
double a,b,c;
System.out.println("Enter three sides of triangle");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
double s = (a+b+c)/2;
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of triangle = "+area);
}
}
Q2) Write a program to accept the length of simple pendulum and calculate the time
period. Display the result.
𝑙
T=2𝜋√ , 𝑤ℎ𝑒𝑟𝑒 𝑔 = 10 𝑚/𝑠 2
𝑔

Ans:
import java.util.*;
public class Simple_Pendulum
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
double l;
System.out.println("Enter the length");
l = sc.nextDouble();
int g = 10;
double t = 2*3.142*Math.sqrt(l/g);
System.out.println("Time Period = "+t);
}
}
Q3) Write a program to accept three integers in x, y, and z. Find and display the
largest number among them.
Ans:
import java.util.*;
public class LargestOfThree
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.println("Enter the value for a, b and c");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
int m = Math.max(Math.max(a,b),c);
System.out.println("Largest of three numbers: "+m);
}
}
Q4) Write a program to accept any two integers. Find and display the smallest number
among them.
Ans:
import java.util.*;
class SmallestNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a,b;
System.out.println("Enter any two numbers");
a = sc.nextInt();
b = sc.nextInt();
int c = Math.min(a,b);
System.out.println("Smallest Number = "+c);
}
}
Variable Description:
Name of Variable Data type Purpose
a int To take input for
first number
b int To take input for
second number
c int To store smallest
number between
a and b.

Q5) Write a program to accept any two-digit number and display the largest digit
present in that number.
Eg:
Input: 36
Output: 6 is the largest digit
Input: 93
Output: 9 is the largest digit
Ans:
import java.util.*;
class LargestDigit
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter any two digit number");
n = sc.nextInt();
int f = n/10; // Store the quotient
int l = n%10; // store the remainder
int c = Math.max(f,l);
System.out.println(c + " is the largest digit.");
}
}
--------------------------------------------------------------------------------------------------------
vii) Math.log(): This function return the natural logarithmic value of a given
argument. It always return a double type value.
Syntax: <double> variable = Math.log(argument);
Eg:
double x = Math.log(10);
= 2.03
viii) Math.round(): This function returns the rounded off form of the given argument.
In case of positive number:
If the fractional part is less than 0.5 then it returns the same value of the integer part
of the argument, otherwise it returns the next higher integer value. This function
returns the value in either long or int. If the argument passed is a double type value
then the return type will be long and if the argument passed is a float type value then
the return type will be int.
Eg:
long x = Math.round(3.4);
=3
long x = Math.round(3.5):
=4
int a = Math.round(4.2f):
=4
int a = Math.round(4.5f);
=5
In case of negative number:
If the fractional part lies between 0 (inclusive) and 0.5 (inclusive) then the function
return the same integer value, otherwise it return lower integer value.
long x = Math.round(-3.4);
= -3
long x = Math.round(-3.5);
= -3
long x = Math.round(-3.52);
= -4
int a = Math.round(-3.52f);
= -4
ix) Math.rint(): This function returns the truncated value (integral part) of a number. It
always return an integer value in double data type. However it predicts the different
outputs for positive and negative numbers.
Syntax: <double> variable = Math.rint(argument);
In case positive number:
If the fractional part of the number lies from 0 to 0.5, then it returns the same integer
in double form. If the fractional part is more than 0.5 then it returns the next higher
integer of the number.
Eg:
double x = Math.rint(4.3);
= 4.0
double x = Math.rint(4.5);
= 4.0
double x = Math.rint(4.51);
= 5.0
In case of negative number:
If the fractional part of the number is less than or equal to 0.5 then it returns the same
integer in double form. If the fractional part is more than 0.5 then it returns the next
lower integer in double form.
Eg:
double k = Math.rint(-4.4);
= -4.0
double k = Math.rint(-4.5);
= -4.0
double k = Math.rint(-4.6);
= -5.0
x) Math.ceil(): It returns the smallest integer which is greater than or equal to the
argument. It always return the value in double data type.
Syntax: <double> variable = Math.ceil(argument);
In case of positive numbers:
If the fractional part of the number is more than 0 then it returns the next higher
integer value in double form.
Eg:
double p = Math.ceil(4.0);
=4
double p = Math.ceil(4.01);
= 5.0
double p = Math.ceil(4.999999);
= 5.0
In case of negative numbers:
It always gives the same integer in double form.
Eg:
double p = Math.ceil(-4.0);
= -4.0
double p = Math.ceil(-4.9999999);
= -4.0
double p = Math.ceil(-0.2345);
= -0.0
xi) Math.floor(): This function returns largest integer that is less than or equal to the
argument. It always return the value as a double data type.
Syntax: <double> variable = Math.floor(argument);
In case of positive numbers:
It will give the same integer in double form.
Eg:
double x = Math.floor (0.23);
= 0.0
double x = Math.floor (4.9);
= 4.0
double x = Math.floor(16);
= 16.0
In case of negative number: It gives the next lower integer.
Eg:
double p = Math.floor (-0.0);
= -0.0
double p = Math.floor(-0.01);
= -1.0
double p = Math.floor(-16.999);
= -17.0
double p = Math.floor(-16);
= -16.0
xii) Math.random(): This function returns random number between 0 and 1.
Lowest value that it can give is 0.0
Highest value that it can give is 0.999999….
It returns the value in double.
Syntax: <double> variable = Math.random();

Modification with random function:


(i) To get random integer number between 1 and n.
int x = (int) (Math.random()*n)+1;
eg:
Random integer between 1 and 5.
int x = (int) (Math.random()*5)+1;
(ii) To get random integer number between any two given numbers, say m and
n, where m is lower limit and n is my upper limit:
int x = (int) (Math.random()*(n-m))+m;
eg:
To get random number between 5 and 8.
int x = (int) (Math.random()*(8-5))+5;
=============================================================
Questions
Q1) What values will be stored in the variables p and q after executing the following
codes?
(i) double a = -6.35; double b = 14.74;
double p = Math.abs(Math.ceil(a));
double q = Math.rint(Math.max(a,b));
Ans:
p = Math.abs(Math.ceil(-6.35));
= Math.abs(-6.0);
= 6.0
q = Math.rint(Math.max(-6.35,14.74));
= Math.rint(14.74);
= 15.0
(ii) double b = -15.6;
double p = Math.rint(Math.abs(b));
= Math.rint(15.6);
= 16.0
(iii) int p = Math.abs(Math.min(-61, -79));
int p = Math.abs(-79)
= 79
(iv) double q = Math.cbrt(64.0);
= 4.0
(v) double a = -99.51, b = -56.25;
double p = Math.min(Math.floor(a), b);
= Math.min(-100.0, -56.25);
= -100.0
Q.) Write the output for the following:
a) Math.round(Math.pow(2.4,2));
= Math.round(5.76)
=6
b) Math.abs(Math.max(-8,-11));
= Math.abs(-8)
=8
c) Math.abs(Math.floor(6.5));
= Math.abs(6.0)
= 6.0
d) Math.max(Math.ceil(5.9),6.5);
= Math.max (6.0, 6.5);
= 6.5
e) Math.ceil(14.8);
= 15.0
Exercise
Q1. State whether the following statements are True or False:
a) The return data type of Math.log() is double. ___________
b) Java.Math class is used for different mathematical functions. _____________
c) The output of Math.abs(-99.99) is 100.0 ____________
d) Math.sqrt(-225) can’t be defined. ___________
e) The output of Math.cbrt(-3) will be -27. ____________
f) Math.round() returns the value in float data type. ___________
Q2. Predict the output:
a) System.out.println (Math.floor(-4.7));
b) System.out.println (Math.rint(-9.4) + Math.sqrt(9.0)); = -9.0 + 3.0 = -6.0
c) System.out.println (Math.max(Math.ceil(14.55),15.5);
= Math.max(15.0,15.5)
= 15.5
d) System.out.println(Math.ceil(3.4) + Math.pow(2,3));
e) System.out.println(Math.sqrt(Math.min(42.5, 42.25)));
= Math.sqrt(42.25)
= 6.5
Q3. Write down the syntax for the following functions:
a) To find the natural log of q.
double x = Math.log(q);
b) To find the cube root of a number m.
double k = Math.cbrt(m);
c) To find the round off a number k.
long x = Math.round(k);
d) To find the absolute value of a number y.
int z = Math.abs(y);
e) To find the exponent of a number p.
double k = Math.pow(a,p);
Q4) Explain the following functions:
a) Math.random()
b) Math.pow()
c) Math.cbrt()
d) Math.log()
Q5) Distinguish between:
a) Math.ceil() and Math.floor()
b) Math.rint() and Math.round()
Programs:
Q1) Write a program to accept the time in seconds, display it in hour:minute:second
format.
Q2) Write a program to accept any three numbers and display the smallest number
among them.
Q3) In a math examination, each correct answer is 6 marks and for incorrect -3 marks
is deducted. Anil has attempted all question and he scored 45 marks. If he has
answered 12 correct questions. Find:
(i) Number of incorrect questions attempted
(ii) Total number of questions present in the exam.
Write a program in java to solve this problem.
If 12 questions are correct then marks = 12 *6 = 72
But , he got 45 marks
Marks lost = 72 – 45
= 27
1 question wrong = -3
For 27 marks, he must have made incorrect question = 27/3 = 9
Total number of questions in exam = 12 + 9
= 21
Ans 3:
class IncorrectQuestion
{
public static void main(String [] args)
{
int cm = 6, im = 3;
int marksToGet = 12*cm;
int marksLost = marksToGet - 45;
int incorrect = marksLost/im;
System.out.println("Number of incorrect questions = "+incorrect);
int total = 12 + incorrect;
System.out.println("Total Question in exam = "+total);

}
}
H.W:
1) Write a Java program to calculate the Body Mass Index (BMI) of a person. The BMI is calculated
as weight in kilograms divided by the square of height in meters.

Ans:

import java.util.*;

class BMI

public static void main(String [] args)

Scanner sc = new Scanner(System.in);

int w, h;

System.out.println("Enter weight in kiligram");

w = sc.nextInt();

System.out.println("Enter height in metres");

h = sc.nextInt();

double bmi = (double)w/(h*h);

System.out.println("BMI of a person = "+bmi);

2) Write a Java program to calculate the simple interest. The formula for simple interest is
𝑃∗𝑅∗𝑇
𝑆𝐼 = 100
, where P is the principal, R is rate of interest, T is time period.

Ans:

import java.util.*;

public class SimpleInterest

public static void main(String [] args)


{

Scanner sc = new Scanner(System.in);

double p,r,t;

System.out.println("Enter Principal, rate and time");

p = sc.nextDouble();

r = sc.nextDouble();

t = sc.nextDouble();

double SI = (p*r*t)/100;

System.out.println("Simple Interest = "+SI);

3) Write a Java program to calculate the distance between two points (x1,y1) and (x2,y2) Use the
distance formula:
D = √(𝑥2 − 𝑥1)2 + (𝑦2 − 𝑦1)2
D= Math.sqrt(Math.pow((x2-x1),2) +Math.pow((y2-y1),2))

Ans:

import java.util.*;

public class DistanceFormula

public static void main(String [] args)

Scanner sc = new Scanner(System.in);

int x1,x2,y1,y2;

System.out.println("Enter the value for x1,x2,y1 and y2");

x1 = sc.nextInt();

x2 = sc.nextInt();

y1 = sc.nextInt();

y2 = sc.nextInt();

double D = Math.sqrt(Math.pow((x2-x1),2) +Math.pow((y2-y1),2));

System.out.println("Distance = " +D + " Km");

}
4) Write a java program to calculate the compound interest using the given formula:
𝑅
𝐶𝐼 = 𝑃(1 + 100)𝑇 − 𝑃; Where P is the Principal, R is Rate of Interest, T is Time Period.
Ans:
import java.util.*;
public class CompoundInterest
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
double p,r,t;
System.out.println("Enter p, r and t ");
p = sc.nextDouble();
r = sc.nextDouble();
t = sc.nextDouble();
double CI = p*(Math.pow((1+r/100),t)) - p;
System.out.println("Compound Interest = "+CI);
}
}
H.W Qno 5 to 7
5) Write a Java program to convert temperature from Fahrenheit to Celsius.
Formula of Fahrenheit to Celsius is given by:
5
𝐶 = (𝐹 − 32)
9
6) Write a Java program to calculate the area of circle by taking the input for radius.
Formula of Area of Circle = 𝜋𝑟 2
7) Write a program to input the cost price and selling price of an article. Assume selling price is
more than the cost price. Calculate and print the profit percent.

You might also like