remainder() in C++ program


In this article, we will be discussing the working, syntax, and examples of remainder() function in C++.

What is remainder()?

remainder() function is an inbuilt function in C++ STL, which is defined in <cmath> header file. remainder() is used to find the remainder of the parameters.

This function takes two arguments, one for the numerator and second for the and computes its remainder and returns a floating-point which is rounded to the nearest.

This function computes −

remainder = numerator – roundquot * denominator;

In which “remainder” is the resulting numerator being the first argument and denominator is the second argument and roundquot is the rounded off (toward the even number) result of numerator/denominator.

This function is similar to the fmod function which rounds of the quotient towards zero.

Syntax

double remainder(double numerator, double denominator);

Parameters

The function accepts the following parameter(s) −

  • numerator − The value of quotient’s numerator.

  • denominator − Quotient’s denominator.

Return value

This function returns the remainder of numerator and denominator if the numerator is zero the function will also return zero. When the denominator is zero either the function returns zero or gives a domain error.

Input 

remainder(51, 5);

Output 

1

Example

 Live Demo

#include <cmath>
#include <iostream>
using namespace std;
int main(){
   int numerator = 51;
   int denominator = 5, remainder_ans;
   remainder_ans = remainder(numerator, denominator);
   cout<<"Value of Remainder is " <<numerator << "/" << denominator << " is: " << remainder_ans << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

Value of Remainder is 51/5 is: 1

Example

//when the denominator is 0 output will be −

 Live Demo

#include <cmath>
#include <iostream>
using namespace std;
int main(){
   int numerator = 51;
   int denominator = 0, remainder_ans;
   remainder_ans = remainder(numerator, denominator);
   cout<<"Value of Remainder is " << numerator << "/" << denominator << " is: " <<
   remainder_ans << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

Value of Remainder is 51/0 is: -2147483648 //garbage value

Updated on: 22-Apr-2020

215 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements