Thursday, 10 September 2020

Part One: Exercise 5

 In this exercise, we would look at arithematic operations with Pyhthon 3. We could do additions, subtraction multiplication, division and so on.

We are going to write a little program that takes two numbers as input from the user and then performs and prints out the addition, the subtraction, the multiplication, the division, the integer division (ignores the remainder), the modulus (only returns the remainder), the exponent (the power).

We can compare the numbers to see if the are equal using the double equal sign so that Python returns a True or a False statement.

You can compare with my solution below:

print('*' * 50) #prints * 50 times

print('A PROGRAM TO PERFORM ARITHEMATIC OPERATIONS')

print('*' * 50)

print()         #prints a blank line

firstnumber = float(input('Please enter the first number: ')) #user enters the first number

print()      

secondnumber = float(input('Please enter the second number: ')) #user enters the second number

print()

 

addition = firstnumber + secondnumber  #adds the two numbers

subtraction = firstnumber - secondnumber  #substracts the two numbers

multiplication = firstnumber * secondnumber  #multiplies the two numbers

division = firstnumber / secondnumber  #divides the two numbers

integerdivision = firstnumber // secondnumber  #ignores the remainder

modulus = firstnumber % secondnumber  #returns only the remainder

power = firstnumber ** secondnumber  #first number raised to power second number

compare = firstnumber == secondnumber #checks if first number equals second number

print('-' * 30)

print(f'Addition is {addition}')

print('-' * 30)

print(f'Subtraction is {subtraction}')

print('-' * 30)

print(f'Multiplication is {multiplication}')

print('-' * 30)

print(f'Division is {division}')

print('-' * 30)

print(f'Integer Division is {integerdivision}')

print('-' * 30)

print(f'Modulus is {modulus}')

print('-' * 30)

print(f'Raised to Power is {power}')

print('-' * 35)

print(f'Is the 1st and 2nd number equal? {compare}')

print('=' * 35)

 

Notice that I used float for the inputs. This is to account for the fact that the user might enter decimal numbers and also not to get errors with division.

No comments:

Post a Comment