Numbers Exercises#

Question-1: Triple Root#
In a single line of code, compute \(\sqrt{\sqrt{\sqrt{625}}}\) and round it to the nearest hundredth.
Question-2: Log Expression#
In at most two lines of code, compute \(\displaystyle |ln( 2^{sin(|-100|)})|\) and round it to the nearest hundredth.
Question-3: Function Evaluation#
Write a program that prompts the user for three numbers using three input functions. Assign the entered values to variables with the names x, y, and z.
Find \(\displaystyle f(x, y,z) = \frac{5xy}{2+x^2}+\frac{x+y+z}{y^4+x^2y^2}\)
Round this value to the nearest hundredth and print the result in the following format.
Sample Output:
x: 1
y: 2
z: 3
f(1,2,3)=3.63
Question-4: Height Conversion#
Write a program that prompts the user to enter their height using two input() functions for the feet and inch parts separately.
Assign the entered values to variables named feet and inch.
Convert the given height into centimeters using the following conversion formulas: 1 foot = 12 inches and 1 inch = 2.54 cm
Sample Output:
Enter the feet part of your height: 6
Enter the inch part of your height: 4
6 feet and 4 inches = 193.04 cm
Question-5: Digit Pair Swap#
Write a program that prompts the user for a 4-digit positive number. Swap the first two digits of the given number with the last two digits and print it.
Do not use string indexing that will be covered in the next chapter.
Example 1: If the given number is 1234, then print 3412.
Print format: 1234 —- swap—> 3412
Example 2: If the given number is 6789, then print 8967.
Print format: 6789 —- swap—> 8967
Question-6: Distance#
In a coordinate plane, each point is represented by its x and y components in the form of \((x,y)\).
The distance between two points \(P=(x_1,y_1)\) and \(Q=(x_2,y_2)\) is given by the following distance formula:
\(\displaystyle dist(P,Q) = \sqrt{(x_2-x_1)^2+(y_2-y_1)^2}\)
Write a program that prompts the user for the x and y components of a point using two input() functions and computes the distance between the given point and the point \((-5,6)\).
Round the distance to the nearest hundredth.
Sample Output:
Enter the x-component of the point: -2
Enter the y-component of the point: 10
The distance between (-5,6) and (-2.0,10.0) is 5.0
Question-7: Midpoint#
In a coordinate plane, each point is represented by its x and y components in the form of \((x,y)\).
The midpoint \((x_m, y_m)\) between two points \(P=(x_1,y_1)\) and \(Q=(x_2,y_2)\) is given by the following midpoint formula:
\(\displaystyle x_m = \frac{x_2+x_1}{2}, y_m = \frac{y_2+y_1}{2}\)
Write a program that prompts the user for the x and y components of a point using two input() functions, and computes the x and y components of the midpoint between the given point and the point \((-5,6)\)
Sample Output:
Enter the x-component of the point: 3
Enter the y-component of the point: 5
The midpoint between (-5,6) and (3.0,5.0) is (-1.0,5.5).
Question-8: Pythagorean#
The Pythagorean Theorem states that for a right triangle with sides \(a\), \(b\), and \(c\) where \(c\) is the hypotenuse: \(\displaystyle c = \sqrt{a^2+b^2}.\)
Write a program that prompts the user for the sides of a right triangle using two input() functions, and computes the length of the hypotenuse.
Round the hypotenuse to the nearest hundredth.
Sample Output:
Enter the length of the first side: 4
Enter the length of the second side: 8
The length of the hypotenuse: 8.94
Question-9: Degrees to Radians#
An angle in degrees is converted to radians using the following formula: \(\displaystyle R = \frac{\pi}{180} \cdot D\).
Write a program that prompts the user for an angle in degrees using an input() function, converts it to radians, and rounds it to the nearest hundredth.
Use the conversion formula provided above, and also the math.radians() function to compare the results.
Sample Output:
Enter the angle in degrees: 120
120.0 degrees is 2.09 using the formula.
120.0 degrees is 2.09 using the math.radians() functions.
Question-10: Law of Cosines#
The Law of Cosines states that for a triangle with sides \(a\), \(b\), and the angle \(\gamma\) between these two sides:
\(\displaystyle c = \sqrt{a^2+b^2-2ab \cdot \cos(\gamma)}\),
where \(c\) is the length of the third side of the triangle corresponding to the angle \(\gamma\).
Write a program that prompts the user for two sides of a triangle and the angle between them in degrees using three input() functions, and computes the length of the third side \(c\).
Round \(c\) to the nearest hundredth.
The math.cos() function takes the angle in radians. You need to convert the given angle in degrees to radians before using it.
Sample Output:
Enter the length of the first side : 6
Enter the length of the second side : 3
Enter the angle between these two sides in degrees: 70
The length of the third side: 5.72
Question-11: Rational Function#
Write a program that prompts the user for a number. Assign the entered value to variable with the name x.
Find \(\displaystyle f(x) = \frac{x^3-2x}{x+6}\)
Round this value to the nearest hundredth and print the result in the following format.
Sample Output:
x: 10
f(10)=61.25
Question-12: Change Maker#
Write a program that prompts the user for an integer amount of money in cents. Your program should calculate how many:
100-cent coins
25-cent coins
10-cent coins
5-cent coins
1-cent coins are needed to represent that amount using the largest possible coins first.
Then print the number of each type of coin on separate lines.
Question-13: Weighted Average#
Write a program that prompts the user for:
the first grade
the weight of the first grade
the second grade
the weight of the second grade
The program should calculate the weighted average using the formula:
\(\displaystyle average = \frac{(g_1 \times w_1) + (g_2 \times w_2)}{w_1 + w_2}\)
Then print the weighted average rounded to two decimal places.
Solution
Question-14: Nearest Integer#
Write a program that prompts the user for a positive floating-point number.
Your program should compute the nearest integer value to the given number without using the round() function.
Then print the result.
Question-15: Digit Rotation#
Write a program that prompts the user for a 3-digit positive integer.
Your program should rotate the digits one position to the right.
The last digit becomes the first digit.
The other digits shift one place to the right.
Do not use string operations.
Print the result in the following format:
original_number —- rotate —> rotated_number
Question-16: Time Difference#
Write a program that prompts the user to enter:
the starting time in hours
the starting time in minutes
the ending time in hours
the ending time in minutes
Assume all values are valid and represent times on the same day.
Your program should:
Convert both times into total minutes.
Compute the difference in minutes.
Convert the difference back into hours and minutes.
Print the result in the format shown below.
Time difference: XX hours and XX minutes.