Sunteți pe pagina 1din 22

The Math Class

Chapter 8:

Practice 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.
2. Math.sqrt( ) function is used to find the square of a number.
3. All trigonometric functions works with angles specified in radians.
4. Math.abs( ) function is used to round off a real number to the nearest
integer.
5. Math. rint(12.5) will return 12.0.
6. Math.ceil( ) and Math.floor( ) are same.
7. NaN stands for Not a Number, which is received when you pass a
negative number to the Math.sqrt( ) function.
8. Math.cosec( ) function is the reciprocal of Math.sin( ) function.
9. Math.cot(x) function is used as a reciprocal of Math.tan(x) function.
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]
[True]
[True]
[False]
[True]
[False]
[True]
[False]
[False]

[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:
i) If x = 9.99, calculate Math.abs(x);
ii) If x = 9.0, calculate Math.sqrt(x);

[ICSE 2008]

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)
d) Math.pow(4,3)
e) Math.sqrt(625)
g) Math.log10(1000)
h) Math.rint(14.5)
j) Math.floor(15.36)
k) Math.round(146.5)
m) Math.min(14.3,14.3)
n) Math.rint(14.562)

Ans. a)
d)
g)
j)
m)

-127.0
64.0
3.0
15.0
14.3

b)
e)
h)
k)
n)

45.6
25.0
14.0
147
15.0

c) Math.min(0.0,0.0)
f) Math.cbrt(125)
i) Math.ceil(12.56)
l) Math.max(11,11)
o) Math.ceil(14.0)

c)
f)
i)
l)
o)

-0.0
5.0
-12.0
11
-14.0

4. Write equivalent Java expressions for the following:

Ans. a)
b)
c)
d)
e)

Math.sqrt(a+b)
1/3.0*a*a*a+1/4.0*b*b*b
s=u*t+1/2.0*a*t*t
d=Math.sqrt(l*l+b*b)
-b+Math.sqrt(b*b-4*a*c)/(2*a)
-b-Math.sqrt(b*b-4*a*c)/(2*a)
Math.pow(Math.sin(x),2)+Math.pow(Math.cos(y),2)
(Math.tan(a)+Math.tan(b))/(1-Math.tan(a)*Math.tan(b))
(1-Math.log10(x))/(1+Math.log10(x))
Math.cbrt(5*x*x+Math.sin(y))/Math.pow(Math.cos(x)+11*y*y*y,1/4.0)
(Math.asin(x)+Math.acos(y))/Math.sqrt(Math.log(Math.cos(x)*Math.cos

f)
g)
h)
i)
j)
(x)))
k)
Math.atan(x)+Math.cos(y)*Math.cos(y)-2*x*x*y*y*yMath.sqrt(4*Math.exp(2*x))
l) Math.sqrt(2*a*s+u*u)

B. Write programs for the following:


1. Write a program to input the area of a square and find its perimeter.

Ans. import java.util.*;


class Question1
{
static void main()
{
double a,s,p;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the area of a square:");
a=sc.nextDouble();
s=Math.sqrt(a);
p=4*s;
System.out.println("Perimeter="+p);
}
}
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:

Ans. import java.util.*;


class Question5
{
static void main()
{
int a,b,c;
double area,s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the three sides of a triangle:");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a<b+c && b<a+c && c<a+b)
{
if(a==b && b==c)
{
System.out.println("It is an equilateral triangle");
area=Math.sqrt(3)/2*a*a;
}
else if(a*a==b*b+c*c || b*b==a*a+c*c || c*c==a*a+b*b)
{

if(a*a==b*b+c*c)
area=1/2.0*b*c;
else if(b*b==a*a+c*c)
area=1/2.0*a*c;
else
area=1/2.0*a*b;
System.out.println("It is a right-angled triangle");
}
else
{
s=(a+b+c)/2.0;
area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("It is a scalene triangle");
}
System.out.println("Area of the triangle="+area);
}
else
System.out.println("Triangle cannot be formed");
}
}
6. Write a program to calculate the values of X, where
() + ()
X=+
+ () ()
Where the value of a ranges from 0 to 90 and the corresponding value of b is 90 a.

Ans. class Question6


{
static void main()
{
double a,b,x,numerator,denominator;
for(a=0;a<=90;a++)
{
b=90-a;
numerator=Math.tan(Math.toRadians(a))+Math.tan(Math.toRadians(b));
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:

Ans. import java.util.*;


class Question9
{
static void series_i()
{
int x,n,i;
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++)
{
sum+=Math.pow(x,i)/i;
}
System.out.println("Answer="+sum);
}
static void series_ii()
{
int x,n,i;
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++)
{
sum+=Math.pow(x,i)/(i+1);
}
System.out.println("Answer="+sum);
}
static void series_iii()
{
int x,n,i;
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++)
{
sum+=Math.pow(x,i+1)/i;
}
System.out.println("Answer="+sum);
}
static void series_iv()
{
int x,n,i;
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++)

{
if(i%2==0)
sum-=Math.pow(x,i)/i;
else
sum+=Math.pow(x,i)/i;
}
System.out.println("Answer="+sum);
}
static void series_v()
{
int x,n,i;
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++)
{
if(i%2==0)
sum-=Math.pow(x,i)/(i+1);
else
sum+=Math.pow(x,i)/(i+1);
}
System.out.println("Answer="+sum);
}
static void series_vi()
{
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)/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);
}
static void series_ix()
{
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;
if(i%2==0)
sum-=Math.pow(x,i)/f;
else
sum+=Math.pow(x,i)/f;
}
System.out.println("Answer="+sum);
}
static void series_x()
{
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;
if(i%2==0)
sum-=Math.pow(x,i)/f;
else
sum+=Math.pow(x,i)/f;
}
System.out.println("Answer="+sum);
}
static void series_xi()
{
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;
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:

Ans. import java.util.*;


class Question10
{
static void series_i()
{
int n,i,t,c;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the the value of n:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
t=i;
c=0;
while(t!=0)
{
c++;
t=t/10;
}
s=s+Math.log(i/Math.pow(10,c))/i;
}
System.out.println("Sum="+s);
}

static void series_ii()


{
int n,i,t,c;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the the value of n:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(i%2==0)
s=s-Math.log10(i);
else
s=s+Math.log10(i);
}
System.out.println("Sum="+s);
}
}
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");
}
}
13. A number n is said to be a sunny number if + is equal to an integer. For
example, 8 is sunny as + = which is an integer. Write a program to print all
sunny numbers between 1 to100.

Ans. class Question13


{
static void main()
{
int i;
for(i=1;i<=100;i++)
{
if(Math.sqrt(i+1)==(int)Math.sqrt(i+1))
System.out.println(i);
}
}
}
14. Write a program to input an angle (say n) and find its:
i) TAN(n)
ii) SIN(n)
iii) COS(n)

according to the users choice.

Ans. import java.util.*;


class Question14
{
static void main()
{
Scanner sc=new Scanner(System.in);
int n,ch;
System.out.println("Enter the value of n:");
n=sc.nextInt();
System.out.println("Enter 1 to find TAN(n)");
System.out.println("Enter 2 to find SIN(n)");
System.out.println("Enter 3 to find COS(n)");
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("TAN(n)="+Math.tan(n));
break;
case 2:
System.out.println("SIN(n)="+Math.sin(n));
break;
case 3:
System.out.println("COS(n)="+Math.cos(n));
break;
default:
System.out.println("Wrong Choice!");
}
}
}
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 users 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.

Ans.

import java.util.*;
class Question17
{

static void main()


{
Scanner sc=new Scanner(System.in);
int ch,a,b,c;
float d;
System.out.println("MENU");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.print("Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
a=1+(int)(Math.random()*100); //generating a random number between 1 to 100
b=1+(int)(Math.random()*100); //generating a random number between 1 to 100
System.out.print("What is "+a+"+"+b+"=?");
c=sc.nextInt();
if(c==a+b)
System.out.print("Right");
else
{
System.out.println("Incorrect-please try again");
System.out.print("What is "+a+"+"+b+"=?");
c=sc.nextInt();
if(c==a+b)
System.out.print("Right");
else
System.out.print("Wrong "+a+"+"+b+"="+(a+b));
}
break;
case 2:
a=1+(int)(Math.random()*100); //generating a random number between 1 to 100
b=1+(int)(Math.random()*100); //generating a random number between 1 to 100
System.out.print("What is "+a+"-"+b+"=?");
c=sc.nextInt();
if(c==a-b)
System.out.print("Right");
else
{
System.out.println("Incorrect-please try again");
System.out.print("What is "+a+"-"+b+"=?");
c=sc.nextInt();
if(c==a-b)
System.out.print("Right");
else
System.out.print("Wrong "+a+"+"+b+"="+(a-b));
}
break;
case 3:
a=1+(int)(Math.random()*100); //generating a random number between 1 to 100
b=1+(int)(Math.random()*100); //generating a random number between 1 to 100
System.out.print("What is "+a+"*"+b+"=?");
c=sc.nextInt();
if(c==a*b)

System.out.print("Right");
else
{
System.out.println("Incorrect-please try again");
System.out.print("What is "+a+"*"+b+"=?");
c=sc.nextInt();
if(c==a+b)
System.out.print("Right");
else
System.out.print("Wrong "+a+"*"+b+"="+(a*b));
}
case 4:
a=1+(int)(Math.random()*100); //generating a random number between 1 to 100
b=1+(int)(Math.random()*100); //generating a random number between 1 to 100
System.out.print("What is "+a+"/"+b+"=?");
c=sc.nextInt();
if(c==(float)a/b)
System.out.print("Right");
else
{
System.out.println("Incorrect-please try again");
System.out.print("What is "+a+"/"+b+"=?");
c=sc.nextInt();
if(c==(float)a/b)
System.out.print("Right");
else
System.out.print("Wrong "+a+"/"+b+"="+(float)(a/b));
}
break;
default:
System.out.println("Wrong Choice!");
}
}
}

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");
}
}

S-ar putea să vă placă și