Skip to content

Exercise 3 of 10 · Numbers and arithmetic

Add Up Two Prices

What you will make

A small script that adds two prices together and prints the total.

The one new idea: Arithmetic operators (+, -, *, /)

Doing math on stored values is one of the most common things any program does — from totaling a shopping cart to calculating a score — and R's arithmetic operators work the way you'd expect from ordinary math.

Go straight to the code ↓

The concept

Once a number is stored in a variable, you can use it in arithmetic just like you would a literal number. R supports + for addition, - for subtraction, * for multiplication, and / for division, and they follow the same precedence rules you learned in school (multiplication and division happen before addition and subtraction, unless parentheses say otherwise). The result of an arithmetic expression is just another value, which you typically store in a new variable with <- so you can print or reuse it later.

A common source of bugs isn't the math itself, but picking the wrong operator for what you actually mean to compute — subtracting when you meant to add, for instance, produces a program that runs perfectly fine and just gives the wrong answer.

Worked example

r
hours <- 2
minutes <- hours * 60
cat("Total minutes: ", minutes, "\n", sep = "")
Output
Total minutes: 120

Here, hours holds 2, and multiplying it by 60 converts it into minutes, stored in a new variable called minutes. The cat() call then prints that computed value inside a sentence. Nothing about hours or the literal 60 needed to change — only the operator (*) determined what relationship the two numbers had to each other.

Your turn

The starter code is supposed to add two prices together to get a total, but the line that computes total uses the wrong operator — it subtracts one price from the other instead of adding them. Find that line and change the operator so the two prices are summed, matching what the surrounding comment and the "Total: $" message both describe.

If something goes wrong

If the printed total looks too small, that's a sign an operator is doing the wrong job — trace through the math by hand using the actual numbers in the code (15 and 9) and compare that to what each operator would produce: 15 - 9 is 6, while 15 + 9 is 24. If the output is missing entirely or you see an error, check that the line still ends by assigning the result to total with <-, and that the variable names in the cat() call weren't accidentally changed.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Changing the values of price1 or price2 instead of the operator.
The prices themselves are correct as given; the bug is which operation combines them, not what the individual prices are.
Writing price1 + price2 but forgetting to reassign it to total.
If the expression isn't stored back into total with <-, the cat() call still prints the old, incorrect value because total was never updated.
Assuming R prints a $ sign automatically for numbers.
R has no built-in currency formatting — the "$" in the output only appears because it's typed literally as part of the string in the cat() call.

Want a blank editor instead? Open the R playground.