Q. Modulus for float could be achieved by?

  • (A) mod(p, q);
  • (B) fmod(p, q);
  • (C) modulus(p, q);
  • (D) p % q
πŸ’¬ Discuss
βœ… Correct Answer: (B) fmod(p, q);
Explanation:

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.

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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
284
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Akash Lawaniya
Publisher
πŸ“ˆ
93%
Success Rate