C library - fmod() function



The C library fmod() function takes two parameters(x & y) of type double that returns the remainder on division(x/y).

In simple, it calculate the floating-point remainder of division of two numbers.

Syntax

Following is the syntax of the C library function fmod()

double fmod(double x, double y)

Parameters

This function accepts only two parameters −

  • x − This is the floating point value with the division numerator.

  • y − This is the floating point value with the division denominator.

Return Value

This function returns the remainder of dividing x/y.

Example 1

The C library program illustrates the usage of fmod() function.

#include <stdio.h>
#include <math.h>

int main() {
   float p = fmod(13, -7);
   float q = fmod(9, 3);
   float r = fmod(7, 10);
   float s = fmod(5, 2);

   printf("%f %f %f %f", p, q, r, s);
   return 0;
}

Output

On execution of above code, we get the following result −

6.000000 0.000000 7.000000 1.000000

Example 2

Here, we use two float values along with one integer which demonstrates the operation of remainder using fmod().

#include <stdio.h>
#include <math.h>

int main () {
   float a, b;
   int c;
   a = 9.2;
   b = 3.7;
   c = 2;
   printf("Remainder of %f / %d is %lf\n", a, c, fmod(a,c));
   printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b));
   
   return(0);
}

Output

The above code produces the following result −

Remainder of 9.200000 / 2 is 1.200000
Remainder of 9.200000 / 3.700000 is 1.800000

Example 3

The given program has two floating value(x,y) which is passed to the function fmod() to calculate its remainder and display the result.

#include <stdio.h>
#include <math.h>

int main() {
   double x = 10.5, y = 6.5;
   double result = fmod(x, y);
   printf("Remainder of %f / %f == %f\n", x, y, result);
   return 0;
}

Output

After executing the code, we get the following result −

Remainder of 10.500000 / 6.500000 == 4.000000
Advertisements