Starting from:

$24.99

CS104 Lab 4 Solution

1. Implement two methods named up and down to produce the following output:

*
***
*****
*******
*********
*********
*******
*****
***
*


2. Implement two methods named up2 and down2 that produce the following output:

oooooooo/
oooooo/
oooo/
oo/
/
/
/oo
/oooo
/oooooo
/oooooooo


3. Write a method named factorial which takes as parameter an integer n and prints its factorial.

For n=5, you should have the following output:
Factorial of 5 is 120


4. Write two methods which calculate and print the area and volume of a sphere. For a sphere with radius r, the surface area is given by 𝐴 = 4𝜋𝑟2and the volume is given by 𝑉 = 4 𝜋𝑟3.
3

Your methods should take the radius as parameter.
Declare π=3.14159 as constant.

5. Write a method named combination which calculates and prints number of the k-combination of an nelement set. The formula for combination is given by:

𝑛!
𝐶(𝑛, 𝑘) =
(𝑛 − 𝑘)! 𝑘!

Hint: Use the factorial method you have previously written.

6. You will implement a guess game where computer picks a random integer between (including) 1 and 100. The player must then continue to guess numbers until the player guesses the correct number. For every guess, the computer will either say "Too high" or "Too low", and then ask for another input.

You can use “randint” method by importing random library via import random. “randint(x, y)” returns a random integer between x and y where both x and y are included.

(Try to print also the number of guesses)

Here is an example run:
50
Too low
70
Too low
80
Too high
76
Too low
79
Too high 78 Correct!

More products