remainder() in C++

This function is also used to return the remainder(modulus) of 2 floating point numbers mentioned in its arguments.The quotient computed is rounded.

remainder = number – rquot * denom

Where rquot is the result of: numer/denom, rounded toward the nearest integral value (with halfway cases rounded toward the even number).

Syntax :

double remainder(double a, double b)
float remainder(float a, float b)
long double remainder(long double a, long double b)
Parameter:
a and b are the values 
of numerator and denominator.


Return:
The remainder() function returns the floating 
point remainder of numerator/denominator 
rounded to nearest.

Error or Exception : It is mandatory to give both the arguments otherwise it will give error – no matching function for call to ‘remainder()’ like this.

# CODE 1

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate 
// remainder() function
#include <cmath>
#include <iostream>
  
using namespace std;
  
int main()
{
    double a, b;
    double answer;
  
    a = 50.35;
    b = -4.1;
  
    // here quotient is -12.2805 and rounded to nearest value then
    // rquot = -12.
    // remainder = 50.35 – (-12 * -4.1)
    answer = remainder(a, b);
  
    cout << "Remainder of " << a << "/" << b << " is " << answer << endl;
  
    a = 16.80;
    b = 3.5;
  
    // here quotient is 4.8 and rounded to nearest value then
    // rquot = -5.
    // remainder = 16.80 – (5 * 3.5)
    answer = remainder(a, b);
  
    cout << "Remainder of " << a << "/" << b << " is " << answer << endl;
  
    a = 16.80;
    b = 0;
    answer = remainder(a, b);
    cout << "Remainder of " << a << "/" << b << " is " << answer << endl;
  
    return 0;
}

chevron_right


OUTPUT :

Remainder of 50.35/-4.1 is 1.15
Remainder of 16.8/3.5 is -0.7
Remainder of 16.8/0 is -nan

# CODE 2

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate 
// remainder() function
#include <cmath>
#include <iostream>
  
using namespace std;
  
int main()
{
    int a = 50;
    double b = 41.35, answer;
  
    answer = remainder(a, b);
    cout << "Remainder of " << a << "/" << b << " = " << answer << endl;
  
    return 0;
}

chevron_right


OUTPUT :

Remainder of 50/41.35 = 8.65


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.