Recap - Simple Calculator
Part of the Fundamentals section of Coddy's Ruby journey — lesson 23 of 88.
Challenge
EasyYou are provided with the following variables:
operator = "*"
num1 = 12
num2 = 4Build a simple calculator that performs the correct arithmetic operation based on the operator value.
Use if-else statements to check the operator and output the result:
- If
operatoris"+", print the sum ofnum1andnum2 - If
operatoris"-", print the difference (num1 - num2) - If
operatoris"*", print the product - If
operatoris"/", print the quotient (num1 / num2)
Output only the numeric result.
Try it yourself
operator = "*"
num1 = 12
num2 = 4
# TODO: Write your code below
# Use if-else statements to check the operator and perform the correct operation
# Output the result using putsAll lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False