Activity 4: Solve Math Problems

Arithmetic operators are useful for doing calculations in your sketch.  In this activity, we’ll focus on the basics: assignment (=), addition (+), subtraction (-), multiplication (*), division(/), and modulus (%, the remainder of a division calculation).

  • Open up the Arduino Language Reference, and take a look at the list of Arithmetic Operators.

The next example sketch, SimpleMath, adds the variables a and b together and stores the result in c.  It also displays the result in the Serial Monitor. 

Notice that c is now declared as an int, not a char variable type.  Another point, int c = a + b uses the assignment operator (=) to copy the result of the addition operation that adds to b.  The figure below shows the expected result of 89 + 42 = 131 in the Serial Monitor.

Output of the SimpleMath sketch in the Serial Monitor

  • Enter, save, and upload SimpleMath to your Arduino.
  • Check the result in the Serial Monitor. Is it correct?
// Robotics with the BOE Shield - SimpleMath

void setup()

  int a = 89;
  int b = 42;
  int c = a + b;
  Serial.print("a + b = ");

void loop()
  // Empty, no repeating code.

Fit your variables to the result values you need to store. 
This will use less memory so you can write larger sketches that will execute more efficiently.

If you need to work with decimal point values, use float.

If you are using integer values (counting numbers),  choose byte, int, or long.

If your results will always be an unsigned number from 0 to 255, use byte.

If your results will not exceed –32,768 to 32,767, an int variable can store your value.

If you need a larger range of values, try a long variable instead.  It can store values from  ‑2,147,483,648 to 2,147,483,647.

Your Turn – Experiment with Other Arithmetic Operators

You still have , *, /, and % to try out!

  • Replace the addition (+) operator with the subtraction () operator and re-upload the sketch to the Arduino.  (You’ll want to replace both instances of + in the sketch.)
  • Upload and verify the result in the Serial Monitor.
  • Repeat for the multiplication ( *), division (/) and modulus ( % ) operators.