## Python Programming Exercises Ask User For 4 Numbers Absolute Value

1. Show the result of evaluating each expression. Be sure that the value is in the proper form to indicate its type (int, long int, or float). If the expression is illegal, explain why.

2. Translate each of the following mathematical expressions into an equivalent Python expression. You may assume that the math library has been imported (via import math).

3. Show the list of numbers that would be generated by each of the following range expressions.

4. Show the output that would be generated by each of the following program fragments.

print i*i

for j in range(0, y, x): print j, print x + y print "done"

for i in range(1, 11): ans = ans + i*i print i print ans

5. Write a program to calculate the volume and surface area of a sphere from its radius, given as input. Here are some formulas that might be useful: V — 4/3mr3 A — 4m2

6. Write a program that calculates the cost per square inch of a circular pizza, given its diameter and price. A - mr2

7. Write a program that determines the molecular weight of a hydrocarbon based on the number of hydrogen, carbon, and oxygen atoms. You should use the following weights:

Atom Weight

C 12.011

### O 15.9994

8. Write a program that determines the distance to a lighting strike based on the time elapsed between the flash and the sound of thunder. The speed of sound is approximately 1100 ft/sec and 1 mile is 5280 ft.

9. The Konditorei coffee shop sells coffee at $10.50 a pound plus the cost of shipping. Each order ships for $0.86 per pound + $1.50 fixed cost for overhead. Write a program that calculates the cost of an order.

10. Two points in a plane are specified using the coordinates (x1,y1) and (x2,y2). Write a program that calculates the slope of a line through two (non-vertical) points entered by the user, m — ^¿Jxl

11. Write a program that accepts two points (see previous problem) and determines the distance between them. d= sj{x2 -xl)2 + (y2 -y\)2

12. The Gregorian Epact is the number of days between Jan. 1st and the previous 1st quarter moon phase. This value is used to figure out the date of Easter. It is calculated by these formulas (using int arithmetic): C — year/100 epact = (8+ (C/4) - C+ ((8C+ 13)/25) + 11(yea/%19))%30 Write a program that prompts the user for a 4-digit year and then outputs the value of the epact.

13. Write a program to calculate the area of a triangle given the length of its three sides a, b, and c. S = 2±I>±£ A = y/s(s - a) (s - b) (s - c)

14. Write a program to determine the length of a ladder required to reach a given height when leaned against a house. The height and angle of the ladder are given as inputs, len —

15. Write a program to find the sum of the first n natural numbers, where the value of n is provided by the user.

16. Write a program to find the sum of the squares for the first n natural numbers.

17. Write a program to sum a series of numbers entered by the user. The program should first prompt the user for how many numbers are to be summed. It should then input each of the numbers and print a total sum.

18. Write a program that finds the average of a series of numbers entered by the user. As in the previous problem, the program will first ask the user how many numbers there are. Note: the average should always be a float, even if the user inputs are all ints.

19. Write a program that approximates the value of p by summing the terms of this series: 4/1 — 4/3 + 4 5 4 7 4 9 4 11 The program should prompt the user for n, the number of terms to sum and then output the sum of the first n terms of this series.

20. A Fibonacci sequence is a sequence of numbers where each successive number is the sum of the previous two. The classic Fibonacci sequence begins: 1, 1, 2, 3, 5, 8, 13 Write a program that computes the nth Fibonacci number where n is a value input by the user. For example, if n = 6, then the result is 8. Note: Fibonacci numbers grow very rapidly; your program should be able to handle very large numbers.

21. You have seen that the math library contains a function that computes the square root of numbers. In this exercise, you are to write your own algorithm for computing square roots. One way to solve this problem is to use a guess-and-check approach. You first guess what the square root might be and then see how close your guess is. You can use this information to make another guess and continue guessing until you have found the square root (or a close approximation to it). One particularly good way of making guesses is to use Newton's method. Suppose x is the number we want the root of, and guess guess~\——

is the current guessed answer. The guess can be improved by using-as neX( guess.

Write a program that implements Newton's method. The program should prompt the user for the value to find the square root of (x) and the number of times to improve the guess. Starting with a guess value of x/2, your program should loop the specified number of times applying Newton's method and report the final value of guess. You might also print out the value of math.sqrt(x) for comparison.

## Post a comment