Recap - Simple Calculator
Part of the Fundamentals section of Coddy's R journey — lesson 24 of 78.
Challenge
EasyBuild 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
operatoris"+", print the result ofa + b - If
operatoris"-", print the result ofa - b - If
operatoris"*", print the result ofa * b - If
operatoris"/", print the result ofa / b - If
operatoris 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 outputAll lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values