Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to use fmod() function to get the floating-point remainder (modulo) of the division x/y in C

3 Answers

0 votes
#include <stdio.h>     
#include <math.h>
 
int main(int argc, char **argv)
{	
	printf("fmod(4.3, 2.0) = %.1f\n", fmod (4.3, 2.0));
	printf("fmod(18.5, 4.2) = %.1f\n", fmod(18.5, 4.3));
	printf("fmod(5.1, 3.0) = %.1f\n", fmod(5.1, 3.0));
    printf("fmod(-5.1, 3.0) = %.1f\n", fmod(-5.1, 3.0));
    printf("fmod(5.1, -3.0) = %.1f\n", fmod(5.1, -3.0));
    printf("fmod(-5.1, -3.0) = %.1f\n", fmod(-5.1, -3.0));
    printf("fmod(0.0, 1.0) = %.1f\n", fmod(0, 1.0));
    printf("fmod(-0.0, 1.0) = %.1f\n", fmod(-0.0, 1.0));
    printf("fmod(3.1, INFINITY) = %.1f\n", fmod(3.1, INFINITY));
	
    return 0;
}

/*
run:
  
fmod(4.3, 2.0) = 0.3
fmod(18.5, 4.2) = 1.3
fmod(5.1, 3.0) = 2.1
fmod(-5.1, 3.0) = -2.1
fmod(5.1, -3.0) = 2.1
fmod(-5.1, -3.0) = -2.1
fmod(0.0, 1.0) = 0.0
fmod(-0.0, 1.0) = -0.0
fmod(3.1, INFINITY) = 3.1

*/

 



answered Mar 18, 2016 by avibootz
edited Mar 18, 2016 by avibootz
0 votes
#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 13.0;
    double y = 4.7;

    double modulus = fmod(x, y);

    printf("%lf\n", modulus); 
    
    return 0;
}

   
/*
run:
 
3.600000

*/

 



answered Sep 1, 2017 by avibootz
0 votes
#include <stdio.h>
#include <math.h>

int main(void)
{
    printf("%f\n", fmod(1, 0.1));
    printf("%2.19f\n", fmod(1, 0.1));
    
    return 0;
}

   
/*
run:
 
0.100000
0.0999999999999999500

*/

 



answered Sep 1, 2017 by avibootz
...