Chapter -1.7 Math class
Chapter -1.7 Math class
7: The Math
Practice
Class Questions
-------------------------------------->Objective-Type Questions<---------------------------------
State True or False:
1. It is possible to find the square root of a number using the Math.pow( )
function. [True]
2. Math.sqrt( ) function is used to find the square of a number. [True]
3. All trigonometric functions works with angles specified in radians. [True]
4. Math.abs( ) function is used to round off a real number to the nearest
integer. [False]
5. Math. rint(12.5) will return 12.0. [True]
6. Math.ceil( ) and Math.floor( ) are same. [False]
7. NaN stands for Not a Number, which is received when you pass a
negative number to the Math.sqrt( ) function. [True]
8. Math.cosec( ) function is the reciprocal of Math.sin( ) function. [False]
9. Math.cot(x) function is used as a reciprocal of Math.tan(x) function. [False]
10. Math.round( ) function returns an int value if the parameter is float
data type and long value if the parameter is of double data type. [True]
---------------------------------->Subjective-Type Questions--------------------------------------
A. Answer the following questions:
1. Name the class that is used for different mathematical functions. Give an
example of a mathematical function. [ICSE 2007]
Ans. Math class. Math.sqrt( )
2. Give the output of the following expressions: [ICSE 2008]
i) If x = –9.99, calculate Math.abs(x);
ii) If x = 9.0, calculate Math.sqrt(x);
Ans. i) 9.99
ii) 3.0
3. Give the output of the following functions:
a) Math.floor(–126.349) b) Math.max(45.6,17.3) c) Math.min(–0.0,0.0)
d) Math.pow(4,3) e) Math.sqrt(625) f) Math.cbrt(125)
g) Math.log10(1000) h) Math.rint(14.5) i) Math.ceil(–12.56)
j) Math.floor(15.36) k) Math.round(146.5) l) Math.max(11,11)
m) Math.min(14.3,14.3) n) Math.rint(14.562) o) Math.ceil(–14.0)
Ans. a) Math.sqrt(a+b)
b) 1/3.0*a*a*a+1/4.0*b*b*b
c) s=u*t+1/2.0*a*t*t
d) d=Math.sqrt(l*l+b*b)
e) -b+Math.sqrt(b*b-4*a*c)/(2*a)
-b-Math.sqrt(b*b-4*a*c)/(2*a)
f) Math.pow(Math.sin(x),2)+Math.pow(Math.cos(y),2)
g) (Math.tan(a)+Math.tan(b))/(1-Math.tan(a)*Math.tan(b))
h) (1-Math.log10(x))/(1+Math.log10(x))
i) Math.cbrt(5*x*x+Math.sin(y))/Math.pow(Math.cos(x)+11*y*y*y,1/4.0)
j) (Math.asin(x)+Math.acos(y))/Math.sqrt(Math.log(Math.cos(x)*Math.cos
(x)))
k) Math.atan(x)+Math.cos(y)*Math.cos(y)-2*x*x*y*y*y-
Math.sqrt(4*Math.exp(2*x))
l) Math.sqrt(2*a*s+u*u)
2. Write a program that outputs the results of the following evaluations based on
the number entered by the user:
i) Natural logarithm of the number
ii) Absolute value of the number
iii) Square root of the number
iv) Random numbers between 0 and 1. [ICSE 2006]
Ans. import java.util.*;
class Question2
{
static void main()
{
double n,l,a,s,r;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:");
n=sc.nextDouble();
l=Math.log(n);
a=Math.abs(n);
s=Math.sqrt(n);
r=Math.random();
System.out.println("Natural Logarithm="+l);
System.out.println("Absolute Value="+a);
System.out.println("Square Root="+s);
System.out.println("Random Number between 0 and 1="+r);
}
}
3. Write a program to calculate the interest and total amount to be paid by entering
the amount of loan and the number of years, either by simple interest method or
by compound interest method, as per the choice of user.
For Simple Interest,
Interst=(Amount*Rate * Number of Years) / 100 and Total amount=Amount +
Interest
For Compound Interest,
𝑹𝒂𝒕𝒆
Total amount = Amount (𝟏 + ) No. of years
𝟏𝟎𝟎
Ans. import java.util.*;
class Question3
{
static void main()
{
double p,r,t,i,a;
int ch;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the principle:");
p=sc.nextDouble();
System.out.println("Enter the rate:");
r=sc.nextDouble();
System.out.println("Enter the time:");
t=sc.nextDouble();
System.out.println("Enter 1 to find the Simple Interest");
System.out.println("Enter 2 to find the Compound Interest");
System.out.print("Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
i=(p*r*t)/100;
a=p+i;
System.out.println("Interest:"+i);
System.out.println("Amount Payable:"+a);
break;
case 2:
a=p*Math.pow((1+r/100),t);
i=a-p;
System.out.println("Interest:"+i);
System.out.println("Amount Payable:"+a);
break;
default:
System.out.println("Invalid Choice!");
}
}
}
4. The following formulae describe the properties of portion of circle with radius ‘r’
and central angle ‘x’ in degrees:
Area = r2
If the values of r and x are entered through the keyboard. Write a program to
calculate the above values of the circle.
Ans. import java.util.*;
class Question4
{
static void main()
{
double r,x,area,arc,chord,segment,y;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of r and x:");
r=sc.nextDouble();
x=sc.nextDouble();
y=Math.toRadians(x); //Convert degrees to radians to use with Math.sin()
area=Math.PI*r*r;
arc=Math.PI*r*x/180;
chord=2*r*Math.sin(y/2);
segment=(Math.PI*r*r*x)/360-(r*r*Math.sin(y))/2;
System.out.println("Area="+area);
System.out.println("Length of arc="+arc);
System.out.println("Length of chord="+chord);
System.out.println("Area of segment="+segment);
}
}
5. Write a program to input the length of the 3 sides of a triangle (say a, b and c)
and calculate the area depending upon the following criteria:
denominator=1+Math.tan(Math.toRadians(a))*Math.tan(Math.toRadians(b));
x=numerator/denominator;
System.out.println(x);
}
}
}
7. Write a program to calculate and print the values of:
𝒙𝟐 + 𝒚𝟐
𝒁=
𝒙+𝒚
Where, x ranges from 0 to 50 and y remains constant at 5. Use symbolic constant
for y.
Ans. class Question7
{
static void main()
{
float x,Z;
final int y=5;
for(x=0;x<=50;x++)
{
Z=(x*x+y*y)/(x+y);
System.out.println(Z);
}
}
}
8. Write a program using conditional operator to input two integers and if the
difference is positive, find the square root of its difference, otherwise find its cube
root.
Ans. import java.util.*;
class Question8
{
static void main()
{
int a,b,d;
double ans;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integers:");
a=sc.nextInt();
b=sc.nextInt();
d=a-b;
ans=(d>0)?Math.sqrt(d):Math.cbrt(d);
System.out.println("Answer="+ans);
}
}
9. Write a program to input two integers, say x and n, and find the sum of the
following series:
sum+=Math.pow(x,i)/f;
}
System.out.println("Answer="+sum);
}
static void series_vii()
{
int i,j,x,n;
long f;
double sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integers:");
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i+1;j++)
f=f*j;
sum+=Math.pow(x,i)/f;
}
System.out.println("Answer="+sum);
}
static void series_viii()
{
int i,j,x,n;
long f;
double sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two integers:");
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
f=f*j;
sum+=Math.pow(x,i+1)/f;
}
System.out.println("Answer="+sum);
}
if(i%2==0)
sum-=Math.pow(x,i)/f;
else
sum+=Math.pow(x,i)/f;
}
System.out.println("Answer="+sum);
}
if(i%2==0)
sum-=Math.pow(x,i)/f;
else
sum+=Math.pow(x,i)/f;
}
System.out.println("Answer="+sum);
}
if(i%2==0)
sum-=Math.pow(x,i+1)/f;
else
sum+=Math.pow(x,i+1)/f;
}
System.out.println("Answer="+sum);
}
}
10. Write a program to find the sum of the following logarithmic series:
11. A projectile fired at an angle , given an initial velocity of vo, will travel a distance
produces a table of the following form for all angles from 30 to degrees in 5
degree increments and initial velocities from 500 to 1000m/sec in 100m/sec
increments:
(INITIAL VELOCITY=500m/sec)
ANGLE DISTANCE TIME HEIGHT
Ans. class Question11
{
static void main()
{
double vo, theta, d,t,h;
final double g=9.8;
vo=500; //initial velocity
System.out.println("ANGLE\t\tDISTANCE\t\t\tTIME\t\t\t\tHEIGHT");
for(theta=30;theta<=90;theta+=5)
{
d=(vo*vo)/g*Math.sin(Math.toRadians(2*theta));
t=2*vo*Math.sin(Math.toRadians(theta))/g;
h=vo*vo/g*Math.sin(Math.toRadians(theta));
System.out.println(theta+"\t\t"+d+"\t\t"+t+"\t\t"+h);
}
}
}
12. A game of dice is to be simulated for two players, each player gets a chance to
throw his dice, and the value is added to his points, this process continues
alternately until for the player whose added points equal to 20 and is declared the
winner. Write a program to simulate this process using the random( ) function.
Ans. class Question12
{
static void main()
{
int d1,d2,s1=0,s2=0;
do
{
d1=1+(int)(Math.random()*6); //dice throw for first player
s1+=d1;//Add the value of the dice to the first players total
if(s1>=20)
break;
d2=1+(int)(Math.random()*6); //dice throw for second player
s2+=d2;//Add the value of the dice to the second players total
if(s2>=20)
break;
}while(s1<20 && s2<20);
if(s1>=20)
System.out.println("First player is the winner");
else
System.out.println("Second player is the winner");
}
}
15. Write a function that simulates rolling a pair of dice until the total on the dice
comes up to be a given number. The number that you are rolling for is a parameter
to the function. The number of times you have to roll the dice is the return value of
the function. You can assume that the parameter is one of the possible totals: 2, 3,
..., 12. Use your function in a program that computes and prints the number of
rolls it takes to get snake eyes. (Snake eyes means that the total showing
on the dice is 2.)
Ans. class Question15
{
static int rollingPair(int n)
{
int d1,d2,c=0;
do
{
c++;
d1=1+(int)(Math.random()*6);
d2=1+(int)(Math.random()*6);
if(d1+d2==n)
break;
}while(true);
return c;
}
static void main()
{
int s;
s=rollingPair(2);
System.out.println("Number of rollings for Snake eyes:"+s);
}
}
16. The roots (say x1 and x2) of a quadratic equation ax2+bx+c=0 can be
determined by its discriminant (d), where d = b2 – 4ac. Depending upon its value
the nature of the roots and the value of the roots can be determined.
i) If d=0 then the roots are Real and Equal and the roots can be found using the
formula:
−𝒃
𝟐𝒂
ii) If d>0 then the roots are Real and Distinct and the roots can be found using the
formula:
−𝒃±√𝐝
𝟐𝒂
iii) If d<0 then the roots are Imaginary and therefore roots are not found.
Write a program to input a,b and c the coefficient of the quadratic equation and
find its roots.
Ans. import java.util.*;
class Question16
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c,d;
double r1,r2;
System.out.println("Enter the coefficients of the quadratic equation:");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
d=b*b-4*a*c;
if(d==0)
{
System.out.println("Roots are Real and Equal");
r1=r2=-b/(2*a);
System.out.println("Roots are:"+r1+"\t"+r2);
}
else if(d>0)
{
System.out.println("Roots are Real and Distinct");
r1=(-b+Math.sqrt(d))/(2*a);
r2=(-b-Math.sqrt(d))/(2*a);
System.out.println("Roots are:"+r1+"\t"+r2);
}
else
{
System.out.println("Roots are Imaginary");
System.out.println("Roots not found");
}
}
}
17. Write a program for all four operations (addition, subtraction, multiplication
and division according to the user’s choice). Each of the problem is to be random.
Also if the user gets a problem incorrect, give him/her a second try at the problem.
If the problem is correct display “Right”, otherwise display “Wrong” after the
second try.
18. Write a program to input a real number (floating point number) and round off
each number to 2 places of decimal and display the answer.
For example, if
INPUT: 12.3867
OUTPUT: 12.39
similarly, if
INPUT: 73.2846
OUTPUT: 73.28
Ans. import java.util.*;
class Question18
{
static void main()
{
float n,f;
int r;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a real number:");
n=sc.nextFloat();
r=Math.round(n*100);//Move the decimal place 2 digits to the right
f=(float)r/100;
System.out.println(f);
}
}
19. Write a program to input 10 numbers and find their sum after converting each
number to its equivalent positive value.
Ans. import java.util.*;
class Question19
{
static void main()
{
float n,s=0;
int i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 numbers:");
for(i=1;i<=10;i++)
{
n=sc.nextFloat();
s=s+Math.abs(n);
}
System.out.println("Sum="+s);
}
}
20. Write a program to input a binary number (consisting of only 1's and 0's) and
convert it to its equivalent decimal.
For example, if
INPUT: 110
OUTPUT: 6
similarly if
INPUT: 1002
OUTPUT: No a proper binary number.
Ans. import java.util.*;
class Question20
{
static void main()
{
int n,s=0,c=0,f=0,d;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a binary number:");
n=sc.nextInt();
while(n!=0)
{
d=n%10;
if(d!=1 && d!=0)
{
f=1;
break;
}
s=s+d*(int)Math.pow(2,c++);
n=n/10;
}
if(f==0)
System.out.println("Decimal Equivalent="+s);
else
System.out.println("Not a proper binary number");
}
}