CMPSC 101 Homework 2 – Conditions and Loops solution

$20.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (4 votes)

Problem 1 (5 points). What will be the output of the following code if:
I. x = 3 and y = 2
II. II. x = 3 and y = 4
III. III. x = 2 and y = 2
if x 2:
if y 2:
z = x + y
print(“z is”, z)
else:
print(“x is”, x)
Problem 2 (10 points). Write a program which asks the user to enter their age and outputs if
they are an infant, a child, a teenager or an adult. Use the following rules:
1) ages 1 or less means the person is an infant
2) ages older than 1 but younger then thirteen means the person is a child
3) ages at least thirteen but younger than twenty means the person is a teenager
4) ages twenty and over means the person is an adult
Sample program output:
Please your age: 14
You are a teenager
Problem 3 (10 points). Write a program which gives the option between doing 3 things:
1) Add two numbers
2) Multiply three numbers
3) Multiply a number and a string
Ask the user to choose an option. Output “Invalid option” if the user enters something apart
from 1, 2 or 3. Otherwise depending on what the user enters ask the user again for inputs to
perform the required choice and output the answer.
Sample program output 1:
You can choose between:
1) Adding two numbers
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
2) Multiplying three numbers
3) Multiplying a number and a string
Please choose an option: 2
Please enter number 1: 2
Please enter number 2: 3
Please enter number 3: 5
The answer is 30
Sample program output 2:
You can choose between:
1) Adding two numbers
2) Multiplying three numbers
3) Multiplying a number and a string
Please choose an option: 3
Please enter a string: hello
Please enter a number: 3
The answer is hellohellohello
Problem 4 (15 points). Analyze the following code. If you (hypothetically) check the condition
count < 100 at the positions in the code designated as # Point A, # Point B, and # Point C, will
the condition evaluate to always True, always False, or sometimes True and sometimes False?
Give your answer for all the three positions.
count = 0
while count < 100:
# Point A
print(“Programming is fun!”)
count += 1
# Point B
# Point C
Problem 5 (10 points). How many times does the body of the while loop repeat? What is the
output of each loop?
i = 1
while i < 10:
if i % 2 == 0:
print(i)
i += 1
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
Problem 5 (15 points). Write a python script to calculates a person’s body mass index (BMI).
The BMI is often used to determine whether a person is overweight or underweight for his or
her height. BMI is calculated with the following formula:
??? =
????ℎ?
(ℎ???ℎ?)
2
× 703
Weight is measured in pounds (lb) and height is measured in inches (in). The program should
ask the user to enter his or her weight and height and then display the user’s BMI. The program
should also display a message indicating whether the person has optimal weight, is
underweight, or is overweight based on the given table.
BMI 25 Overweight
18.5 <= BMI <= 25 Normal
BMI < 18.5 Underweight
Sample program output:
Enter weight (lb): 134
Enter height (in): 66
BMI: 21.62
You are normal.
Problem 6 (10 points). Write a program which asks the user to enter a number and prints its
multiplication table upto 10.
Sample program output:
Please enter a number: 9
9 X 1 = 9
9 X 2 = 18
9 X 3 = 27
9 X 4 = 36
9 X 5 = 45
9 X 6 = 54
9 X 7 = 63
9 X 8 = 72
9 X 9 = 81
9 X 10 = 90
CMPSC 101 Instructor: Ishan Behoora
Introduction to Programming in Python Spring 2017
Problem 7 (10 points). Given a number n calculate n! using a while loop. n!, called n factorial, is
defined as the product of numbers 1 through n.
For example 4! = 1 X 2 X 3 X 4 = 24
For example 2! = 1 X 2 = 2
For example 5! = 1 X 2 X 3 X 4 X 5 = 120
Sample program output:
Please enter n: 3
3 factorial is 6
Problem 8 (15 points). Write a program which asks the user for two numbers and calculates the
first number raised to the second number.
Sample program output:
Please enter number 1: 2
Please enter number 2: 5
2 ** 5 is 32
Note: Do not use the ** operator or the pow() function use loops and multiplication to calculate
the answer
Extra Credit
Problem 9 (25 points). Given a binary number convert the number to its decimal equivalent. If
the number inputted by the use contains any digits apart from 0 or 1 output and error message.
Sample program output 1:
Please enter the binary number: 10010
The decimal equivalent of 10010 is 18
Sample program output 2:
Please enter the binary number: 111
The decimal equivalent of 111 is 7
Sample program output 3:
Please enter the binary number: 2019
The decimal equivalent of 2019 is not a binary number