In this C++ tutorial, you will learn how to find the floating point remainder of division of given numerator and denominator using fmod() function of cmath, with syntax and examples.

C++ fmod()

C++ fmod() returns floating point remainder of division of given numerator and denominator.

Syntax

The syntax of C++ fmod() is

fmod(x, y)

where

ParameterDescription
xNumerator value.
yDenominator value.

Returns

The return value depends on the type of value passed for parameters x and y.

The return value of fmod(x, y) is

  • double if x and y are double.
  • float if x and y float.
  • long double if x and y are long double.

The synopsis of fmod() function is

double fmod(double x, double y);
float fmod(float x, float y);
long double fmod(long double x, long double y);
double fmod(Type1 x, Type2 y); // for other combinations of numeric types

fmod() is a function of cmath library. Include cmath library at the start of program, if using fmod() function.

ADVERTISEMENT

Example

In this example, we read two values from user into variables x and y, and find the floating point remainder of x/y operation.

C++ Program

#include <iostream>
#include<cmath>
using namespace std;

int main() {
    double x, y;
    cout << "Enter numerator (x) : ";
    cin >> x;
    cout << "Enter denominator (y) : ";
    cin >> y;
    
    double remainder = fmod(x, y);
    cout << "Remainder, fmod(" << x << ", " << y << ") : " << remainder << endl;
}

Output

Enter numerator (x) : 6.5
Enter denominator (y) : 2.1
Remainder, fmod(6.5, 2.1) : 0.2
Program ended with exit code: 0
Enter numerator (x) : 17.6
Enter denominator (y) : 3.1
Remainder, fmod(17.6, 3.1) : 2.1
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ fmod(), and how to use this function to find floating point remainder of given numerator and denominator, with the help of examples.