Q. Modulus for float could be achieved by?
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.