🎓 A1Learner
🔍
0 of 52 lessons completed

Julia Arithmetic

Arithmetic is working with numbers using operations such as adding and multiplying. Julia does arithmetic with familiar symbols, and each calculation gives a value you can print or store.

The four basic operations

Julia uses + for adding, - for subtracting, and * for multiplying. The / operation divides.

println(3 + 4)
println(10 - 3)
println(2 * 5)
println(7 / 2)

Whole division, remainder, and power

The div function keeps only the whole part of a division. The % symbol gives the remainder, and ^ gives a power.

println(div(7, 2))
println(7 % 2)
println(2 ^ 10)

Mixing whole and decimal numbers

When a whole number and a decimal number meet in one calculation, the result is a decimal number. Even an exact division prints with a decimal point.

println(3 + 0.5)
println(2 * 1.5)
println(6 / 2)

Example

a = 9
b = 4
println(a + b)
println(a / b)
println(div(a, b))
println(a % b)
println(a ^ 2)

Key points

  • Plus and minus: + adds two numbers and - takes one number away from another.
  • Star: * multiplies two numbers.
  • Slash: / divides and always gives a Float64 result, even when the answer is whole.
  • div: div(a, b) divides and keeps only the whole part of the answer.
  • Percent: % gives the remainder left over after whole division.
  • Caret: ^ raises a number to a power, so 2 ^ 3 means 2 multiplied by itself 3 times.

In this example, a holds 9 and b holds 4. The first line adds them to get 13, the second divides them to get 2.25, the third keeps the whole part 2, the fourth prints the remainder 1, and the last raises 9 to the power 2 to get 81.

Note

mixing an Int64 and a Float64 in one calculation gives a Float64 result.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

What does println(17 % 5) print?