C/Math/Mod

Материал из C\C++ эксперт
Перейти к: навигация, поиск

floating-point remainder value function

#include <math.h>
#include <stdio.h>
int main(void)
{
  printf("%1.1f", fmod(10.0, 3.0));
  return 0;
}


Mod and divide

  
#include <stdio.h>
int main(void)
{
  printf("%d", 7/2);
  printf(" %d", 7%2);
  printf(" %d", 4/2);
  printf(" %d", 4%2);
  return 0;
}


Return remainder of floating point division: fmod

#include <stdio.h>
#include <math.h>
int main ()
{
  printf ("fmod of 6.3 / 2 is %lf\n", fmod (6.3,2) );
  printf ("fmod of 19.5 / 4.2 is %lf\n", fmod (19.5,4.2) );
  return 0;
}


Split floating-point value into fractional and integer parts: how to use modf

#include <stdio.h>
#include <math.h>
int main ()
{
  double p, fractpart, intpart;
  p = 3.14159265;
  
  fractpart = modf (p , &intpart);
  
  printf ("%lf = %lf + %lf \n", p, intpart, fractpart);
  
  return 0;
}


Use the mod % operator

#include <stdio.h>
int main(void)
{
  int i;
  printf("Enter a number: ");
  scanf("%d", &i);
  
  if( ( i % 2 ) == 0) 
      printf("Even");
  if( ( i % 2 ) ==1) 
      printf("Odd");
  return 0;
}