Menu
Coddy logo textTech

Recap - Simple Calculator

Part of the Fundamentals section of Coddy's R journey — lesson 24 of 78.

challenge icon

Challenge

Easy

Build a simple calculator that performs different arithmetic operations based on the operator provided.

You are provided with the following variables:

a <- 20
b <- 4
operator <- "*"

Write a series of if-else statements that check the value of operator and perform the corresponding calculation:

  • If operator is "+", print the result of a + b
  • If operator is "-", print the result of a - b
  • If operator is "*", print the result of a * b
  • If operator is "/", print the result of a / b
  • If operator is anything else, print "Invalid operator"

Use the print() function for your output.

Try it yourself

# Variables are already defined
a <- 20
b <- 4
operator <- "*"

# TODO: Write your code below
# Use if-else statements to check the operator and perform the calculation
# Remember to use print() for your output

All lessons in Fundamentals