Skip to main content

Exercises 4.16 Exercises

Remember to work one step at a time! If you are feeling stuck, try to think of the smallest bit of work you can try to do in order to get one step closer to your goal. Then just worry about that one step of work.

1.

Construct a block of code that calculates the volume of a cylinder with a radius of 3 and a height of 4. Store the result into a variable before printing it.

2.

It’s Black Friday and the Nintendo Switch you’be been saving up for is marked down to 60% of its original price! Construct a block of code that calculates how much money you’d be saving if the system originally costed $359.99?

3.

The code below reads in a decimal value. Print out the two integers it is between, smaller value first, with exactly one space in between them. For example, given 1.4 you should print 1 2. Given -2.6 you should print -3 -2.
If it is an integer value already, your program should print out the integer twice.
Hint.
There are rounding functions in the cmath library

4.

This program should read in the opposite and adjacent sides for one of the acute angles in a right triangle and then print the size of the angle in degrees.
Use the rule \(angle = atan(opposite/adjacent)\) to find the measure of the angle.
Hint.
atan produces an answer in radians. Don’t forget to convert your answer to degrees.

5.

Your friend is getting sick of solving quadratic equations by hand. Finish the program below that will take a, b, and c from a quadratic in the form \(ax^2 + bx + c = 0\) and output the two solutions separated by a space.
The two solutions to a quadratic are given by \(x = \frac{-b + \sqrt{b^{2} - 4ac}}{2a}\) and \(x = \frac{-b - \sqrt{b^{2} - 4ac}}{2a}\text{.}\) Do your best to avoid doing the same calculation multiple times β€” use a variable to hold the result \(\sqrt{b^{2} - 4ac}\) (known as the discriminant).
Note: Some quadratics only have one solution or no solutions. But you can assume the quadratic will always have two solutions.
Hint. Hint about debugging
If you have trouble, do the math one step at a time and print out each result along the way. Check them by hand.
You have attempted of activities on this page.