# Thread: help with modulus in C

1. ## help with modulus in C

Hi,
I'm struggling to understand the modulus operator and why it isn't returning what I expect it to return in an equation.

Using the below equation, when I have y down as 420 I get b = 2, however when compiling it within a programme b = 1. Please can someone explain to me why this happens?

b = (y % 25) / 10

Thank you,
Salome 2. No idea. I get 2.
If you're getting 1, post your test case.
Code:
```\$ cat foo.c
#include<stdio.h>

int main(void)
{
int y = 420;
int b = (y % 25) / 10;
printf("b=%d\n",b);
return 0;
}
\$ gcc foo.c
\$ ./a.out
b=2``` 3. Originally Posted by Salem No idea. I get 2.
If you're getting 1, post your test case.
Code:
```\$ cat foo.c
#include<stdio.h>

int main(void)
{
int y = 420;
int b = (y % 25) / 10;
printf("b=%d\n",b);
return 0;
}
\$ gcc foo.c
\$ ./a.out
b=2```
my output wasn't exactly what I wanted so I have narrowed the problem down to 'b' so changed the code so that only 'b' would be printed rather than the actual answer to the problem. I just need to know why I'm getting 1 in my programme for 'b' rather than 2. Thank you for your help.

Code:
```#include <cs50.h>
#include <stdio.h>

float get_positive_float(string prompt);

int main(void)
{
float change_required = get_positive_float("How much change? ");
int y = change_required * 100;
int a, b, c, d, e;

a = y / 25;
b = (y % 25) / 10;
c = (y - (a * 25) - (b * 10)) / 5;
d = y - (a * 25) - (b * 10) - (c * 5);
e = a + b + c + d;

if (y > 0)
{
printf("%d\n", b);
}
}

float get_positive_float(string prompt)
{
float change_required;
do
{
change_required = get_float("%s", prompt);
}
while (change_required <= 0);
return change_required;
}``` 4. Code:
```\$ cat foo.c
#include<stdio.h>

int main(void)
{
int y = 419;  //!! NOT 420
int b = (y % 25) / 10;
printf("b=%d\n",b);
return 0;
}
\$ gcc foo.c
\$ ./a.out
b=1```
> int y = change_required * 100;
Floating point numbers are pesky things at times.
You might think you typed in 4.2, and reasonably expect y to be 420.
But I bet you'll find it's really 4.199999999999999 and you have y=419 as a result. 5. Of course...thank you. So straight forward, but yet not! Popular pages Recent additions #### Tags for this Thread

compiling, equation, modulus, programme, salome 