Arithmetic operators - C programming | Ludu

Arithmetic operators

by Rickard Borg Rickard Borg

2 thanks

  • Arithmetic operators are used frequently in order to calculate and process data in your program. You will use them a lot for different purposes and are important to understand.

    +
  • Let's briefly look at what all operators does and what they look like

    +
  • + adds numbers:

    +
  • Loading code...
    +
  • - subtracts numbers from each other:

    +
  • Loading code...
    +
  • * multiplies values:

    +
  • Loading code...
    +
  • / divides numbers:

    +
  • Loading code...
    +
  • % is called the "modulus operator" and it also performs division. However, it only returns the remainder of what's left:

    +
  • Loading code...
    +
  • Loading code...
    +
  • When it comes to arithmetic, it works exactly like it does in maths. We follow the basic rules of mathematics for calculating.

    +
  • Loading code...
    +
  • Tip: When calculating, it is often a good habit to use paranthesis, even if the program works without them, to avoid making some basic math mistakes. Sometimes you'll want to type (10 + 3 + 5 + 7) instead of just 10 + 3 + 5 + 7 even if it seems obvious to you that they're the same!

    +
  • Exercise

    +
  • Create a program that calculates the avarage value of a bunch of numbers. If your values are 11, 22 and 33, the result should be 22.

    +
  • Remember the arithmetic order for how the calculation works. Paranthesis, multiply/division and addition/subtraction.

    +
    1. 1
      Declare a few variables and set them to a value of your choice

    2. 2

      Store the calculation in another variable

    3. 3

      Print out the result!

    +
  • +
  • Solution

    +
  • Loading code...
    +
  • Loading code...
    +

Did it help you? Click to say "thanks" to the author!

Next lesson: Control the flow

Want to create your own course on Ludu?

Back to lesson