-2
#include<stdio.h>
#include<conio.h>
void main()
{
  float n, r;
  printf("\n enter a number");
  scanf("%d",&n);
  r=n%10;
  n/=10;
  printf("%d %d",n, r );
  getch();
}

This code is showing error while compiling. I want to know: can we perform mod operation on floating point values?

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82

1 Answers1

5

No, you can't use % on float

Use fmod function instead Referenced here: http://www.cplusplus.com/reference/cmath/fmod/

Or fmodf http://en.cppreference.com/w/c/numeric/math/fmod

r = fmod (n, 10)

You need to #include <math.h> to use this function

Also, you should do scanf(%f, &n) to read in the float

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

int main()
{
  float n, r;
  printf("\n enter a number");
  scanf("%f", &n);
  r=fmod(n,10.0);
  //r = fmodf(n, 10.0); thansk to chux
  n /= 10;
  printf("%f %f", n, r );
}
James Maa
  • 1,764
  • 10
  • 20