# CS21 Lab 1: First programs

## Due Saturday, September 16, before midnight

Make sure all programs are saved to your cs21/labs/01 directory!

Write a silly madlib program to reproduce the "roses are red..." poem, asking the user for two colors and an adjective.

Here are two examples (user input in bold):

$python3 poem.py color: orange another color: blue adjective: happy Roses are orange Violets are blue Sugar is happy And so are you!$ python3 poem.py
color: 1
another color: 2

Roses are 1
Violets are 2
Sugar is 3
And so are you!

### 2. Rent Budget Calculation

Imagine you are renting an apartment. Write a simple program, called rent.py, to help calculate costs based on: monthly rent, number of months you will be renting, and number of roommates.

Here are two quick examples (user input in bold):

$python3 rent.py monthly rent:$500
number of months: 10
number of roommates: 0
--------------------------------
your monthly bill: $500.0 your total cost:$ 5000.0

$python3 rent.py monthly rent:$500
number of months: 10
number of roommates: 2
--------------------------------
your monthly bill: $166.6666667 your total cost:$ 1666.6666667

In the first example above, it's just you (no roommates). In the second one, the monthly and total costs are split between you and two other roommates.

### 3. Python Math Operators

Just to get comfortable with some python3 math operators, write a program called ops.py that asks the user for two integer factors, and shows the results of applying /, **, and % to the factors.

Here's a few quick examples (user input in bold):

$python3 ops.py Please enter two integers. Factor #1: 4 Factor #2: 10 4 / 10 = 0.4 4 ** 10 = 1048576 4 % 10 = 4$ python3 ops.py

Factor #1: 4
Factor #2: 3

4 / 3 = 1.3333333333333333
4 ** 3 = 64
4 % 3 = 1

$python3 ops.py Please enter two integers. Factor #1: 17 Factor #2: 3 17 / 3 = 5.666666666666667 17 ** 3 = 4913 17 % 3 = 2 ### 4. Calculating the Nth Fibonacci Number The Fibonacci Sequence is a well-known sequence of numbers where the next number in the sequence is the sum of the previous two. Here's a table of the first 10 Fibonacci numbers: N Fib(N) 0 0 1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 Instead of summing the previous two numbers, there is a formula for calculating the Nth number in the Fibonacci Sequence: $$Fib(N) = (\varphi^N - (-\varphi)^{-N})/\sqrt{5}$$ where phi is: $$\varphi = (1 + \sqrt{5})/2$$ Write a program that asks the user for N and calculates the Nth Fibonacci number. Here are a few examples: $ python3 fib.py
n: 5
Fib( 5 ) = 5.0

$python3 fib.py n: 6 Fib( 6 ) = 8.0$ python3 fib.py
n: 7
Fib( 7 ) = 13.0

\$ python3 fib.py
n: 24
Fib( 24 ) = 46368.0

Note: to use the sqrt() function in your program, you will need to import the math library. If you haven't seen this in class yet, you just need to add import math at the top of your program. You can insert this after the comment at the start of the program but it should be before you define your main function.

Here's an interactive example showing use of the math library:

>>> import math
>>> math.sqrt(4)
2.0
>>> math.sqrt(64)
8.0
>>> math.sqrt(37)
6.082762530298219
>>> 

Each lab has a short questionnaire at the end. Please edit the QUESTIONS-01.txt file in your cs21/labs/01 directory and answer the questions in that file.
Don't forget to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.