Starting from:

$29

Homework 2 – Basic Input & Arithmetica

CMPSCI 101 
Introduction to Programming with Python
Homework 2 – Basic Input & Arithmetic

Problem 1. (10 points)
Assume the variables a,b,c and d have the integer values 25,24,28 and 22 respectively. What will be
the value stored in the variable Res after each of these statements:
1) Res = a+b
2) Res = c*2
3) Res = b/a
4) Res = b –c
5) Res = d // c
Problem 2. (10 Points)
Write assignment statements which perform the following operations with variables a and b.
1) Add 2 to a and assign the result to b
2) Multiply b by 5 and assign the result to a
3) Divide a by 3.14 and assign the result to b
4) Subtract 8 from b and assign the result to a
5) Raise a to the power 3 and assign the result to b
Problem 3. (10 Points)
How would you write the following arithmetic expression in Python? Assume a,b,c,d are integer
variables
4/3(r+34)–9(a+bc)+(3+d(2+a))/a+bd
Problem 4. (10 Points)
Evaluate the following expressions using the order of operators. (Show the steps involved)
1) 9 + 6 * (5 + 7)/3 – 7
2) 19 – 6 / (8 - 3) * 2 - 1
For example evaluating 10 + 3*(8-4) – 5 involve the following steps:
 10 + 3 * 4 – 5
 10 + 12 – 5
 17
Problem 5. (10 Points)
Write a program which displays the following tab separated table
a b a**b
2 3 8
4 5 1024
5 6 15625
Problem 6. (10 Points)
Write a program which asks the user to enter the temperature in Celsius and converts it to
Fahrenheit.
Use the formula F = 5/9C + 32, where F is the temperature in Fahrenheit and C is the temperature in
Celsius.
Sample program output:
Please enter the temperature in Celsius: 9
The temperature in Fahrenheit is: 37
Problem 7. (20 Points)
Write a program which asks the user to enter an integer between 0 and 100 and outputs the sum of
the digits of the number entered.
Hint: You can use the modulo operator to extract digits (93%10 = 3) and the integer division operator to
remove digits (91//10 = 9).
Sample program output:
Please enter a number between 0 and 100: 89
The sum of the digits is: 17
Problem 8. (20 Points)
Write a program which asks the user to enter an 8 digit number and outputs the number reversed.
Hint: You can use the modulo operator to extract digits (93%10 = 3) and the integer division operator to
remove digits (91//10 = 9).
Sample program output:
Please enter a number: 89123231
The reverse is: 13232198
Problem 9. (EXTRA CREDIT/OPTIONAL) (25 Points)
Write a program which asks the user to enter a 6 bit binary number and outputs the number’s decimal
equivalent.
Hint: Converting a number from binary to decimal involves multiplying the bits with increasing powers of
2 and adding the result.
For example for the 2 bit binary number 10 the bits 1 and 0 are multiplied by 2 and 1 respectively
resulting in 2 x 1 + 1 x 0 = 2. Thus the decimal equivalent of 10 is 2.
Similarly for the 4 bit binary number 1011 the bits 1, 0, 1, 1 are multiplied by 8, 4, 2 and 1 respectively
resulting in 8 X 1 + 4 X 0 + 2 X 1 + 1 X 1 = 11. Thus the decimal equivalent of 1011 is 11.
Some other examples are (subscripts indicate the number system used; 2 for binary and 10 for decimal)
(binary) 1112 → 4 X 1 + 2 X 1 + 1 X 1 → 710 (decimal)
(binary)100012 → 16 X 1 + 8 X 0 + 4 X 0 + 2 X 0 + 1 X 1 →1710 (decimal)
(binary) 101002 → 16 X 1 + 8 X 0 + 4 X 1 + 2 X 0 + 1 X 0 → 2010 (decimal)
Sample program output:
Please enter a 6 bit binary number: 101010
The decimal equivalent is: 42

More products