# %(Modulus)

This is a discussion on %(Modulus) within the C Programming forums, part of the General Programming Boards category; I'm a beginner in C and i've started to learn what the function %, modulus is and i'm confused with ...

1. ## %(Modulus)

I'm a beginner in C and i've started to learn what the function %, modulus is and i'm confused with how to use it in a program, and what does it have to do with the random(0);
function.If you could explain it to me it would be very helpful,thank you.

2. Mod gives you the remainder of a division. Say we get a random number:

12345

We run mod-100 on it. That means it gives us:

w = 12345
x = w / 100 (or 123)
y = x * 100 (or 12300
z = w - y (or 45)

That's pretty much what mod does. I broke it up into small steps, but that's what it's doing. So, if you apply the same thing to any random number (replace w's value with whatever you want, and you'll still end up with the 10s and 1s spot of the number (the remainder of division by 100).

Quzah.

3. It's kinda of calculating remainder, it's like 3 % 2 = 1; 100 % 10 = 0; 5 % 2 = 1; 19 % 4 = 3
and it has nothing to do with random(), which is used to generate a random value ( it should be rand() in C, stdlib.h )

4. % is a modulus operator which falls under Arithmetic Operator category. This operator is used to find the remainder. Here is the simple example which uses modulus operator to find the given number is odd or even.

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

int main()
{
int num=0;
printf("Enter any number:");
scanf("%d",&num);
if( num % 2 == 0)
printf("%d is a even number\n",num);
else
printf("%d is a odd number\n",num);
return 0;
}```

5. % is used to find a remainder of integer division.

It's used with rand() to give it an upper limit.
Code:
`int rn = 1 + ( rand() % 100)    // rn will always be 1 - 100`
This is generally a Bad Idea. See here.

6. It's not a bad idea if you don't care how super random it is. For most things anyone here is ever going to do, it's fine. As a matter of fact, the link you cite tells you as much. It helps if you actually read what you link to.

Quzah.

7. Originally Posted by quzah
It's not a bad idea if you don't care how super random it is. For most things anyone here is ever going to do, it's fine. As a matter of fact, the link you cite tells you as much. It helps if you actually read what you link to.

Quzah.

I think that it is better to highlight a more sophisticated approach with some additional complexity, then to insist on a more simple but naive solution.

8. I'm still confused with the % part, like how do u read num % 2? and what does 2 have to do with it?

9. Originally Posted by cuo741
I'm still confused with the % part, like how do u read num % 2? and what does 2 have to do with it?
When dividing integers, C doesn't do decimal answers. 17 divided by 3 is 5 remainder 2. You get the 5 with / and the 2 with %.

10. Huh?
Code:
```#include <stdio.h>

int main()
{
int m,c;

for(c=0;c<100;c++)
{
m = c % 10;
printf("%d\t",m);
}
return(0);
}```
could you explain this to me i dont get it

11. Originally Posted by cuo741
could you explain this to me i dont get it
At this point, I'm pretty sure the answer is "no". Do you not know how division works?

12. so for example, its like rand() % 6 means u want the remainder of rand() / 6?

13. Originally Posted by cuo741
so for example, its like rand() % 6 means u want the remainder of rand() / 6?
Yes.

Lets say we have a roll of fabric 13m long and we want to cut it into sheets 5m long.
Code:
```int main(void) {

int a, b, rem, quot;

a = 13;   // Total length of fabric.
b = 5;    // We want the sheets to be 5m long.

quot = a / b;   // I.e. how many 5m long sheets we can get?
rem = a % b;    // I.e. how many meters of fabric we'd have
// left after we'd made as many 5m long sheets we could?

printf("Sheets: %d \n", quot);
printf("Remainder: %d \n", rem);

return 0;
}```
Write some code an experiment with the operator! There are many application for it.

14. Originally Posted by msh
Yes.

Lets say we have a roll of fabric 13m long and we want to cut it into sheets 5m long.
Code:
```int main(void) {

int a, b, rem, quot;

a = 13;   // Total length of fabric.
b = 5;    // We want the sheets to be 5m long.

quot = a / b;   // I.e. how many 5m long sheets we can get?
rem = a % b;    // I.e. how many meters of fabric we'd have
// left after we'd made as many 5m long sheets we could?

printf("Sheets: %d \n", quot);
printf("Remainder: %d \n", rem);

return 0;
}```
Write some code an experiment with the operator! There are many application for it.
I like the fabric example good job.

15. Did i use it right?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int remainder;
float hat,hat1;

srand((unsigned)time(NULL));

hat=rand();

hat1=rand() % 5;

printf("The remainder of %.2f divided by 5 = %.2f\n",hat,hat1);

system("PAUSE");
return(0);
}```

Page 1 of 2 12 Last
Popular pages Recent additions