Explanation by: Mr. Dubey

In C, the modulus operator % only works with integer types (int, long, etc.). It does not work with floating-point numbers (float or double).

To find the remainder when dividing floating-point numbers, we use the fmod() function from the math.h library.

Syntax:

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

int main() {
double p = 5.5, q = 2.0;
double result = fmod(p, q);
printf("fmod(%.2f, %.2f) = %.2f\n", p, q, result);
return 0;
}

Output:

fmod(5.50, 2.00) = 1.50

Why Other Options Are Incorrect?

(A) mod(p, q);

  • There is no standard mod() function in C for modulus operation.

(C) modulus(p, q);

  • modulus() is not a valid C function.

(D) p % q

  • % cannot be used for floating-point numbers. It works only with integers.

Final Answer:

fmod(p, q); is the correct way to get the modulus of floating-point numbers in C.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.


Question analytics