0% found this document useful (0 votes)
17 views3 pages

Sheet 3

Uploaded by

Omar Hesham
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)
17 views3 pages

Sheet 3

Uploaded by

Omar Hesham
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/ 3

Sheet 3

1. A simple calculator can add, subtract, multiply and divide two


numbers. Write a java program to declare two numbers, read their values
from the user, add them, multiply them, subtract them and calculate the
remainder and the quotient. The program should display the calculations
applied on those two numbers.
Sample output 1:
Enter the first number: 15
Enter the second number: 3
15 + 3 = 18
15 - 3 = 12
15 * 3 = 45
15 / 3 = 5
15 % 3 = 0

Sample output 2:
Enter the first number: 55
Enter the second number: 10
55 + 10 = 65
55 - 10 = 45
55 * 10 = 550
55 / 10 = 5
55 % 10 = 5

2. Write a java program that solves quadratic equations and prints


their roots. Recall that a quadratic equation is a polynomial equation in
terms of a variable x of the form ax 2 +bx+c=0. The formula for solving a
quadratic equation is:
−𝑏 ± √𝑏 2 − 4𝑎𝑐
𝑥=
2𝑎

The program will read from the user, the coefficients a, b, and c, print
the equation in the form ax 2 +bx+c=0 with the values of the coefficients
and finally print the roots of the equation. You may assume that the
equation has two real roots, though mathematically this is not always the
case.
1|Page
Sample output 1:
Enter coefficient a: 2
Enter coefficient b: 3
Enter coefficient c: 1
The form of the equation: 2x^2+3x +1=0
The first root of the equation = -0.5
The second root of the equation = -1.0

Sample output 2:
Enter coefficient a: 2
Enter coefficient b: 5
Enter coefficient c: 2
The form of the equation: 2x^2+5x +2=0
The first root of the equation = -0.5
The second root of the equation = -2.0

3. Write a java program that calculates the distance between two


points on the Cartesian plane. The program will read from the user
integer coordinates x 1 , y 1 of point A and x 2 , y 2 of point B and computes
the distance between points A(x 1 , y 1 ) and B(x 2 , y 2 ) according to the
following equation :
𝑑 = √((𝑥2 − 𝑥1 )2 + (𝑦2 − 𝑦1 )2 )

The program will finally print the coordinates of the points and the
distance between those two points.

Sample output 1:
Enter the values of Coordinates of point A:
x1=2
y1=1
Enter the values of Coordinates of point B:
x2=3
y2=4
The Distance between the two points A(2,1)and B(3,4) equals
3.1622776601683795

Sample output 2:
Enter the values of Coordinates of point A:
x1=1

y1=2
Enter the values of Coordinates of point B:
x2=4
2|Page
y2=6
The Distance between the two points A(1,2)and B(4,6) equals 5.0

4. Write a java program that will ask the user to enter a number, the program
will use the Math library to calculate and print the absolute value, the rounded
value, the ceiling value, the floor value and the number raised to the power of two
value.
Sample output 1:
Enter a decimal number:-2.3512
The absolute value of -2.3512 = 2.3512
The rounded value of -2.3512 = -2
The ceiling of -2.3512 = -2.0
The floor of -2.3512 = -3.0
(-2.3512)^2 = 5.52814144

Sample output 2:
Enter a decimal number: 4.3268
The absolute value of 4.3268 = 4.3268
The rounded value of 4.3268 = 4
The ceiling of 4.3268 = 5.0
The floor of 4.3268 = 4.0
(4.3268)^2 = 18.721198240000003

3|Page

You might also like