Mathematical Methods
Mathematical Methods
√𝑎 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.
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();
}
}
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
int w, h;
w = sc.nextInt();
h = sc.nextInt();
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.*;
double p,r,t;
p = sc.nextDouble();
r = sc.nextDouble();
t = sc.nextDouble();
double SI = (p*r*t)/100;
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.*;
int x1,x2,y1,y2;
x1 = sc.nextInt();
x2 = sc.nextInt();
y1 = sc.nextInt();
y2 = sc.nextInt();
}
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.