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

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?

Shambhu kumar said:   1 decade ago
What is wrong in this code why it will give segmentation fault?

#include <stdio.h>
void main()
{
register int x = 0;
if (x < 2)
{
x++;
main();
}
}

Dhara said:   1 decade ago
What different between fmod & modf?

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.

Pooja said:   1 decade ago
@Vani.

printf inside the printf....firstly there is a abc and also a slace is a character literal so there it work as a character...and 1 char is 1 byte so 4 character print 4...on place of printf("%d",4 );

Due to %d it'll print 4.

RAGU said:   1 decade ago
What is the difference between modf and fmod?

Sri said:   1 decade ago
Why we can use the fmod? instead of that what word is apt for the execution.

Sugandha Singh said:   1 decade ago
It means % operator is use when we are doing integer division to get the remainder?

Chillu said:   1 decade ago
What is the difference between modf and fmod?

DINESH KUMAR I said:   1 decade ago
The output of following program is:

#include<stdio.h>
int main()
{
int a=1;
printf("%d %d %d",a,a++,++a);
return 0;
}
Output : 3 2 3.
But logically 3 2 2.

Can any explain how 3 2 3 comes?


Post your comments here:

Your comments will be displayed after verification.