In: Computer Science
QUESTION 1:
Imagine that the IRA is giving a stimulus package in the COVID-19
season. The package amount is determined by the number of adults in
the family and the total amount of salary the family gets.
Each adult will get 1200 dollars if the total salary is 100000 or less.
Each adult will get 800 dollars if the total salary is 150000 or less.
Write a python program to ask the user the number of adults in the family and the total number of family salary and determine the total amount of stimulus package the IRA will provide for that family.
QUESTION 2:
Write a program that will ask the user length and width of a right triangle and find the area of the right-angled triangle. The formula for finding the area of a right-angle triangle is
ab/2. Also, find out the result if you calculate as (ab)/2. Is it the same? If it is same, why it is the same. If it is not the same, why it is not the same.
ANSWER 1:
PYTHON CODE:
# ask the user the number of adults
adults = int(input('Enter the total number of adults in the family: '))
# ask the user the total family salary
salary = float(input('Total number of family salary: '))
# total_amount variable
total_amount = 0
# Each adult will get 1200 dollars if the total salary is 100000 or less.
# salary > 0 and salary < 100000
if(salary>=0 and salary<=100000):
total_amount = adults * 1200
# Each adult will get 800 dollars if the total salary is 150000 or less.
# salary > 100000 and salary < 150000
if(salary>100000 and salary<=150000):
total_amount = adults * 800
# print the total amount by format
print('Total amount {} of stimulus package the IRA will provide for that family.' .format(total_amount))
PYTHON CODE SCREENSHOT:
OUTPUT:
ANSWER 2:
Find out the result if you calculate as (ab)/2. Is it the same? If it is same, why it is the same?
Answer:
Case 1: When
we are calculate the area of the right-angled triangle by formula
ab/2.
The operator *, and / have the same operation precedence.
a*b is executed first, because it appears to be there first then
ab/2
For example: a = 5 , b = 6
area = a*b/2
area = 5*6/2
area = 30/2
area = 15.0
Case 2: When
we are calculate the area of the right-angled triangle by formula
(ab)/2.
(a*b) is regular parentheses executed first,then (a*b)/2
For example: a = 5 , b = 6
area = (a*b)/2
area = (5*6)/2
area = 30/2
area = 15.0
So both are give the same result.
PYTHON CODE:
# ask the user length of a right triangle
length = float(input('Enter the length of a right triangle: '))
# ask the user width of a right triangle
width = float(input('Enter the width of a right triangle: '))
# find the area of the right-angled triangle
# The formula for finding the area of a right-angle triangle is ab/2
area1 = length * width / 2
# The formula for finding the area of a right-angle triangle is (ab) /2
area2 = (length * width) / 2
# print the area calculating by ab/2
print("Area of the right-angled triangle is:",area1);
# print the area calculating by (ab)/2
print("Area of the right-angled triangle is:",area2);
PYTHON CODE SCREENSHOT:
OUTPUT:
NOTE: If you don't understand any step please tell me.