Common Arithmetic Operators (OCR GCSE Computer Science)

Revision Note

Flashcards
Robert Hampton

Expertise

Computer Science Content Creator

Common Arithmetic Operators

  • To demonstrate the use of common arithmetic operators, three sample programs written in Python are given below
  • Comments have been included to help understand how the arithmetic operators are being used
    • Arithmetic operators #1 - a simple program to calculate if a user entered number is odd or even
    • Arithmetic operators #2 - a simple program to calculate the area of a circle from a user inputted radius
    • Arithmetic operators #3 - a simple program that generates 5 maths questions based on user inputs and gives a score of how many were correctly answered at the end
Python code

# -----------------------------------------------------------------------
# Arithmetic operators #1
# -----------------------------------------------------------------------
# Get the user to input a number
user_input = int(input("Enter a number: "))

# if the remainder of the number divided by 2 is 0, the number is even
if user_input % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

# -----------------------------------------------------------------------
# Arithmetic operators #2
# -----------------------------------------------------------------------
# Get the radius from the user
radius = float(input("Enter the radius of the circle: "))

# Calculate the area of the circle
area = 3.14159 * radius ** 2

# Display the calculated area
print("The area of the circle with radius",radius,"is",area)

# ------------------------------------------------------------------------
# Arithmetic operators #3
# ------------------------------------------------------------------------
# Set the score to 0
score = 0

# Loop 5 times
for x in range(5):
    num1 = int(input("Enter the first number: "))
    operator = input("Enter the operator (+, -, *): ")
    num2 = int(input("Enter the second number: "))
    user_answer = int(input("What is "+str(num1)+str(operator)+str(num2)+"? "))

# Check the answer and update the score
    if operator == '+':
        correct_answer = num1 + num2
    elif operator == '-':
        correct_answer = num1 - num2
    elif operator == '*':
        correct_answer = num1 * num2

    if user_answer == correct_answer:
        score = score + 1
    else:
      print("Sorry that's incorrect.")

print("Your score is:", score)

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.