C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - General Questions (Q.No. 1)
1.
Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ?
rem = 3.14 % 2.1;
rem = modf(3.14, 2.1);
rem = fmod(3.14, 2.1);
Remainder cannot be obtain in floating point division.
Answer: Option
Explanation:

fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point divisions.

Example:


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

int main ()
{
  printf ("fmod of 3.14/2.1 is %lf\n", fmod (3.14,2.1) );
  return 0;
}

Output:
fmod of 3.14/2.1 is 1.040000

Discussion:
141 comments Page 12 of 15.

Keerthi said:   1 decade ago
Nice explanation given by premkumar now its very clear.

JCoder said:   1 decade ago
fmode() is the lib function, returns the reminder of floating point.

Nagesh said:   1 decade ago
Your answer may be depends on the Statement i.e. both % operator as well as fmod() used for taking Reminder %op for integer and fmod() for float.

Harshada said:   1 decade ago
Yes Prem Kumar is right.

Riyaz said:   1 decade ago
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
c=a%b;
printf("%f",c);
}

Hemanth Kumar K said:   1 decade ago
fmod() function with values of floating type is the only operator which can be used to obtain the remainder for floating numbers.

Mounika said:   1 decade ago
Yes renuka is right.

Saikiran said:   1 decade ago
What Prem Kumar comented is correct.

Saikiran said:   1 decade ago
#include<stdio.h>
#include<math.h>

int main()
{
float i,j;
i=3.14;j=2.1;
printf("\ni modulo j is %f",fmod(i,j));
return 0;
}

O/P:
i modulo j is 1.040000

Velmani said:   1 decade ago
% (%d)is used for only integer datatype. % is does not used for float datatype. So if you need mod in deciamal number then you can use fmod(%f) function.


Post your comments here:

Your comments will be displayed after verification.