C++ Remainder

beginner c++11 math

#include <cmath>

In C++ we use the modulo operator (%) to divide two numbers and take the remainder. The modulo operator (%) only works for integer values. To perform a floating point modulo division we use the fmod function in C++.

#include <cmath>
#include <iostream>

int main() {
  auto result = 23 % 5;

  std::cout << result << "\n";

  auto result2 = std::fmod(23.4f, 5.0f);

  std::cout << result2 << "\n";
}
3
3.4


For more C++ By Example, click here.