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

Prerna said:   7 years ago
fmod is not working in GCC compiler. How can I use this in GCC compiler?

Please suggest me to get the output of this.

Prathamesh m. patkar said:   1 decade ago
Yes, because the given values are in floating type so we also want the ans in floating type therefore we use fmod().

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

Swathy said:   1 decade ago
Yes. For int only we can use mod for float we should use fmod the answer should be c only for this problem.
(1)

Sujitha said:   1 decade ago
@Ayapan.

You have declared the fun as fmod(3, 2).

When 3 gets divided by 2 it gives 1.5 with remainder 0.

Ram krishna said:   1 decade ago
Ya. Its nice ans we use % and mod () , fun for int, type but not for float hence fmode is right answer.

Sundar said:   1 decade ago
@Sreedhar

Step by step process:

a = XXX * 10
a = ABC - XYZ * 10
a = 20 - 10* 10
a = 20 - 100
a = -80

Vishal said:   9 years ago
C is the correct answer as % operator don't work on floating point numbers so A is incorrect.

Bharadwaj said:   1 decade ago
Please clarify my doubt.

What is the meaning of "External declaration and Where is use" ?

S.sasindra said:   7 years ago
Option A is also correct becuase;

int rem=3.14/2.1;
printf("%d",rem);
return();


Post your comments here:

Your comments will be displayed after verification.