Starting from:
$35

$29

Laboratory 2 Arithmetic Solution

Learning Objectives:

 
Develop simple Python programs that do input, produce output and do arithmetic.

 
Use the Python math library.




Activities:

1. Download lab2.py from the OAKS grades/dropbox for this course. Write your code within this document.

Demonstrate your code for the activities below to an instructor. For all give meaningful output messages.




2. Sum of threes.

Write a function, sumOfThrees(), that asks the user for an upper bound and calculates the sum of all multiples of three less than or equal to the upper bound. For example, if the upper bound is 15, 16, or 17, the function should calculate the sum of 3, 6, 9, 12 and 15.




3. Multiplication table.

Write a function, muliplicationTable(), that prints a multiplication table for the numbers 1 through 12. Something like:




1 2 3 4 5 6 7 8 9 10 11 12




1: 1 2 3 4 5 6 7 8 9 10 11 12




2: 2 4 6 8 10 12 14 16 18 20 22 24




3: ….






12:….




4. Computing the area of a triangle.

Write a function, triangleArea(), to calculate the area of a triangle given the length of its three sides, a, b, and c, using these formulas:

and

To find a square root import the math library by putting import math in your program (already done in lab2.py). Then use the function math.sqrt(x), where x is the value whose square root you need.




 
Sum of squares.

Write a function sumSquares() that calculates the sum of the squares of the numbers in a given range. The function should allow the user to input the starting and ending numbers within the range. For example if the user inputs 3 as the lower range and 5 as the upper range, the function should return the result of the equation 32+42+52 which is 50.

 




 
Manually finding the power.

Sadly, Phil the philosopher has found himself stranded on a deserted island with a bad version of Python that does not support the ** operator nor the math library function math.pow(). Phil needs to be able to calculate the power of numbers. Write a function for him called power() that asks the user to input a base and an exponent. The function should use a loop to calculate and output the power. Have your output appear in the form of base ^ exponent = answer. So if the user inputs 4 as the base and 3 as the exponent your output should be:




4 ^ 3 = 64

 







9. Upload the modified file lab2.py to your OAKS account.




10. If you left any files on the desktop, remove them. Log off of your computer. (Don't turn it off.)

More products