# Thread: modulus will not work

1. ## modulus will not work

I understand the entire thing about modulus gives you the remainder, yada yada. THis is what I complied, and this is what I get.
Code:
```#include <stdio.h>
#include <math.h>
int main()
{
float num1, num2, mod;

printf("Please type in a number: ");
scanf("%f%",&num1);
printf("Please type in a number: ");
scanf("%f",&num2);
double fmod(double num1, double num2);

printf("%f divided by %f is remainder %f\n", num1, num2, mod);

return 0;
}```
I type cc -lm -o mod2A.out mod2A.c
Code:
``` Please type in a number: 9
Please type in a number: 7
9.000000 divided by 7.000000 is remainder 0.000000```
that isn't the remainder, where did i go wrong????

2. Well, simple enough, you never set the variable mod to anything. I think you are trying to use the modulus operator inside the first scanf() call which is very weird. You would need to take both first numbers in input and only then assign the result of the modulus operator to the variable mod. However, you can only use the modulus operator on integers.

By the way, what in hell are you trying to do with that fmod() declaration ?

3. Get the return value of the function fmod and store in mod. and print mod. more over you are suppose to send only the variables to the function not the variable type along with it as an argument to the function fmod. have a look at the modified code below, it will make you more sense

Code:
```#include <stdio.h>
#include <math.h>
int main()
{
float num1, num2, mod;

printf("Please type in a number: ");
scanf("%f%",&num1);
printf("Please type in a number: ");
scanf("%f",&num2);

mod = fmod(num1,num2);

printf("%.2f divided by %.2f is remainder %.2f\n", num1, num2, mod);

getchar();
return 0;
}

/*my output
Please type in a number: 9
7
Please type in a number: 9.00 divided by 7.00 is remainder 2.00

*/```
ssharish2005

4. Originally Posted by Desolation
Well, simple enough, you never set the variable mod to anything. I think you are trying to use the modulus operator inside the first scanf() call which is very weird. You would need to take both first numbers in input and only then assign the result of the modulus operator to the variable mod. However, you can only use the modulus operator on integers.

By the way, what in hell are you trying to do with that fmod() declaration ?
Assignment for school

5. Originally Posted by ssharish2005
Get the return value of the function fmod and store in mod. and print mod. more over you are suppose to send only the variables to the function not the variable type along with it as an argument to the function fmod. have a look at the modified code below, it will make you more sense

Code:
```#include <stdio.h>
#include <math.h>
int main()
{
float num1, num2, mod;

printf("Please type in a number: ");
scanf("%f%",&num1);
printf("Please type in a number: ");
scanf("%f",&num2);

mod = fmod(num1,num2);

printf("%.2f divided by %.2f is remainder %.2f\n", num1, num2, mod);

getchar();
return 0;
}

/*my output
Please type in a number: 9
7
Please type in a number: 9.00 divided by 7.00 is remainder 2.00

*/```
ssharish2005
Thank you so much, it does make sense. I did leave out the getchar part, becuase that wasn't in the book for the code we had to rewrite, and it still worked. I did the mod before, but I kept thinking i needed the double num1, because that's what it said everywhere else to do it. I never tried the
Code:
`mod = fmod(num1,num2) i kept just trying things like mod = num1 / num2;`
and so on.

6. You might want to consider not using "mod" as a variable name too, as there is a function in the maths library with the same name and it might get a tad confusing.

7. Well if we're going to be concerned with libraries, "mod" isn't an ANSI standard function, nor is it a reserved keyword, so all bets are off.

Quzah.