×

c# · level 1 · unit 3 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

C# is a calculator that never gets tired.

Each does one job: + adds, - takes away, * multiplies and / divides. Brackets go first, then * and /, then + and -, as in maths lessons.

Watch out with /. When both numbers are int, C# gives a whole number and drops the rest, so 7 / 2 is 3. Make one of them a double, like 7.0 / 2, to get 3.5.

% gives the , what is left over after sharing out. Share 7 sweets between 2 friends: each gets 3, and 7 % 2 is the 1 sweet left over.

using System;

Console.WriteLine(2 + 3 * 4);
Console.WriteLine((2 + 3) * 4);
Console.WriteLine(7 / 2);
Console.WriteLine(7.0 / 2);
Console.WriteLine(7 % 2);

C# never rounds up when it drops the rest, so 9 / 10 is 0.