CIS 100 - Introduction To Computer Science: Homework Assignment 2
CIS 100 - Introduction To Computer Science: Homework Assignment 2
CIS 100 - Introduction To Computer Science: Homework Assignment 2
Instructions:
Submit your assignments in word to the blackboard.
The format of the output should be same as specified in the sample output
Input to every program is colored in blue
Programs
1. Correct the syntax errors in the below programs and re-write the correct code (2 points)
a. Incorrect code:
name = int(input("Enter a name:"))
print("Hello", name)
Answer:
name = input("Enter a name:")
print("Hello", name)
b. Incorrect code:
age = int(input("Enter your age:"))
age = 5
print("Hello", name)
Answer:
age = int(input("Enter your age:"))
print("Hello",age)
c. Incorrect code:
age = 5
print(your age is, age)
Answer:
age = 5
print("your age is", age)
d. Incorrect code:
"a" = 5
"b" = 6
print(a,b)
Answer:
(a) = 5
(b) = 6
print(a,b)
2. Write a program that will ask the user to enter the amount of purchase. The program
should then compute the state sales tax (6 percent) and total of the sale (sum of the
amount of purchase and state sales tax). (2 points)
Sample Output:
Enter the amount of purchase: 1200
State sales tax is: 72.0
Total sale is: 1272.0
Code:
amount = int(input("Enter amount of purchase:"))
tax = 1.06
total = (amount * tax)
print("Your total is:",total)
Output:
Enter amount of purchase:1200
Your total is: 1272.0
3. Write a program that prompts the user to provide name and the year of the birth. It
then determines the age of a person from the year of birth and should display the
output as shown in the sample output. (Ignore the month and day of the birth). If the
year of birth is 1990 then the age could be calculated as 2020 – 1990, which is 30 years.
(1 point)
Sample Output:
Enter your name:Mahesh
Enter the year:1998
Hello Mahesh
Your age is 22
Code:
name = input("Enter you name:")
year = int(input("Enter your birth year:"))
age = 2021 - year
print("Hello", name)
print("Your age is", age)
Output:
Enter you name:Jersey
Enter your birth year:2002
Hello Jersey
Your age is 19
4. Write a program to calculate the surface area of a sphere. The program should prompt
the user to enter the radius of a sphere and then calculate the surface area of the
sphere. (1 point)
The surface area of a sphere is given by the formula, area = 4∏r2. (Use ∏ = 3.14)
Sample Output:
Enter the radius of a surface: 10
Surface area of the sphere is: 1256.0
Code:
radius = int(input("Enter radius:"))
PI = 3.14
area = 4 * PI * radius * radius
print("Surface area of the sphere is:", area)
Output:
Enter radius:10
Surface area of the sphere is: 1256.0
5. Write a program to evaluate the arithmetic operations (+, -, *, /, //, **). The program
should prompt the user to enter two positive numbers and then apply all the arithmetic
operations on those two numbers. (2 points)
Sample Output:
Enter first number: 5
Enter second number: 2
Addition: 7
Subtraction: 3
Multiplication: 10
Division: 2.5
Integer Division: 2
Remainder: 1
Exponent: 25
Code:
num1 = int(input("Enter number 1:"))
num2 = int(input("Enter number 2:"))
Addition = num1 + num2
Subtraction = num1 - num2
Multiplication = num1 * num2
Division = num1 / num2
IntegerDivision = num1 // num2
Remainder = num1 % num2
Exponent = num1 ** num2
print("Addition:",Addition)
print("Subtraction:",Subtraction)
print("Division:",Division)
print("Multiplication:",Multiplication)
print("IntegerDivision:",IntegerDivision)
print("Remainder:",Remainder)
print("Exponent:",Exponent)
Output:
Enter number 1:5
Enter number 2:2
Addition: 7
Subtraction: 3
Division: 2.5
Multiplication: 10
IntegerDivision: 2
Remainder: 1
Exponent: 25