In: Computer Science
*Please give the answers in pseudo code
1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero.
2) Design an algorithm that will read two numbers and an integer
code from the screen. The value of the integer code should be 1, 2,
3 or 4. If the value of the code is 1, compute the sum of the two
numbers. If the code is 2, compute the difference (first minus
second). If the code is 3, compute the product of the two numbers.
If the code is 4, and the second number is not zero, compute the
quotient (first divided by second). If the code is not equal to 1,
2, 3 or 4, display an error message. The program is then to display
the two numbers, the integer code and the computed result to the
screen.
3) Design an algorithm that will receive the weight of a parcel and
determine the delivery charge for that parcel. Charges are
calculated as follows:
Parcel weight (kg)
Cost per kg ($)
<2.5
$3.50 per kg
2.5-5 kg
$2.85 per kg
>5 kg
$2.45 per kg
#Here's the python psedu code for the question
1)
# Take inputs
int1 = raw_input("Enter first number:")
int2 = raw_input("Enter second number:")
# Try summing the inputs; if inputs are not numbers the program will throw error saying the input are not numbers
try:
sum = int(int1) + int(int2)
except:
print ("Enter valid integer inputs")
# Print output sum
print("Sum:" + str(sum_no))
# Now repeat the same block for subtraction and multiplication
# For division check if the denominator is zero or not with if-else condition
if (int(int2) == 0):
print("Denominator for division is zero, division cant be performed)
else:
print ("Division:" + str(int(int1)/int(int2))
2 )
#The answer to second question is similar to the first only with the difference that now you are giving option to user what operation he has to perform instead of printing out all the operations. This can be accomplished by a simple if-else statements.
#So after taking inputs from raw_input function of python check what is the input given like :
if(int(input1) == 1):
# perform addition like in the first part
elif (int(input1) ==2):
#perform subtraction
elif( int(input1) == 3):
#perform multiplication
elif (int(input1) == 4):
#perform division with a non-zero check on denominator
else:
print("Non-valid input given")
3)
# Take input weight
weight = raw_input("Enter weight in kg:")
# calculate cost with simple if-elif-else blocks by multiplying the input weight with cost per kg
if( float(weight) < 2.5):
cost = float(weight) * 3.5
print ("Cost in dollars:" + str(cost))
elif( float(weight) >= 2.5 and float(weight) < 5):
cost = float(weight) * 2.85
print ("Cost in dollars:" + str(cost))
# Like-wise take decision about cost per kg multiplier based on the range of the input