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 3 of 15.

Chandra said:   1 decade ago
% operator represents Reminder in C Language
But it is not possible for floating point operations.So,the Answer would be option (d).

(d)Remainder cannot be obtain in floating point division.

Anitha said:   1 decade ago
Hai friends, mod() is used for the integer division to get remainder. Where as fmod() is used to get floating point remainder. We find both mod() and fmod()in math.h header file check it once.

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

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

I am getting output as 0. Anyone can explain it why?

Manjunath K said:   1 decade ago
Hai friends, mod() is used for the integer division to get remainder. Where as fmod() is used to get floating point remainder. We find both mod() and fmod()in math.h header file check it once.

Prashant said:   1 decade ago
% in c language represents remainder after division,

The number can be of integer or float but in global declaration reminder should be declared in float irrespective of declared variables.

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

int main ()
{
float 3.13.2.1;
float R;
R=fmod(3.14,2.1);
printf ("The remainder is:",R);
scanf("%f",R);
return 0;
}

Krishna said:   1 decade ago
modf(x, y) is used to split the float point in to the intpart and factpart.

Ex: 3.13=3.00+0.13

fmod(x,y) is used to find the remainder in floating points.

Ex: fmod(3.14,2.1)

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

Vidya said:   9 years ago
Usually % (modules) operator not for floating point numbers, so in c fmod is inbuilt function that will use to get the reminder of the floating point number. Answer is C.

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

int main ()
{
int rem;
rem=3.14/2.1;

printf ("rem= %d\n", rem);
return 0;
}

0p:1.


Post your comments here:

Your comments will be displayed after verification.