A

Admin • 802.91K Points
Coach

Q. Modulus for float could be achieved by?

  • (A) mod(p, q);
  • (B) fmod(p, q);
  • (C) modulus(p, q);
  • (D) p % q
  • Correct Answer - Option(B)
  • Views: 8
  • Filed under category C Programming
  • Hashtags:

Explanation by: Admin

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
", 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.

Be the first to start discuss.