fmod() function in C++ and its uses
In this section, we will learn the concept of fmod() function and understand its implementation with an example.
fmod() function in C++
- In C++ there are various types of inbuilt function which helps us to perform a mathematical operation on floating-point numbers.
- The fmod() function is one of those inbuilt functions in the <
cmath
> header file which is used to return the remainder or the modulus of two floating-point numbers. It accepts 2 numbers as numerator and denominator and calculates the reminder rounded towards zero. - The function returns the value of type double which is the floating-point modulus of the division.
Syntax:
double fmod(double a, double b); //Where a = Numerator and b = Denominator //The function returns the reminder of dividing a/b.
Note: To perform a valid operation using fmod() function, the denominator value should always be greater than 0, otherwise it will return a null value.
Example program for fmod() function
Let’s understand the use of fmod() with an example:
#include <iostream> #include <cmath> using namespace std; int main() { double a,b,reminder; //Input the value of numerator and denominator cout << "Enter the numerator value:" << endl; cin >> a; cout << "Enter the denominator value:" << endl; cin >> b; reminder = fmod(a, b); //function call cout << "Remainder of " << a << "/" << b << " = " << reminder << endl; //Let's take the denominator 0 b = 0; reminder = fmod(a, b); //function call cout << "Remainder of " << a << "/" << b << " = " << reminder << endl; return 0; }
Output:
Enter the numerator value: 7.8 Enter the denominator value: 9.4 Remainder of 7.8/9.4 = 7.8 Remainder of 7.8/0 = -nan
Hope this article has helped you to understand the fmod( ) function and its uses in C++.
Happy Coding!!!
You can also read, Mathematical functions in C++
Leave a Reply