C Tutorial C References

C - arithmetic operators example



The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/) and modulo(%) operators.

#include <stdio.h>
 
int main (){
  float a = 25;
  float b = 10;

  printf("a = %f, b = %f \n\n", a, b);

  //Add a and b
  float result_add = a + b;
  printf("a + b = %f\n", result_add);

  //Subtract b from a
  float result_sub = a - b;
  printf("a - b = %f\n", result_sub);

  //Multiply a and b
  float result_mul = a * b;
  printf("a * b = %f\n", result_mul);

  //Divide a by b
  float result_div = a / b;
  printf("a / b = %f\n", result_div);

  //return division remainder - works with integral operands,
  //for other types math.h's fmod function can be used
  int result_modulo = (int) a % (int) b;
  printf("a %% b = %d\n", result_modulo);  
  return 0;
}

The output of the above code will be:

a = 25.000000, b = 10.000000 

a + b = 35.000000
a - b = 15.000000
a * b = 250.000000
a / b = 2.500000
a % b = 5

❮ C - Operators