# Best way to parse an integer and what exactly is the mod operator

This topic is 3689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need a quick way to parse an integer into its separate numbers. I know I can do this the robust way, turn it into a string parse the string and reconvert those back into integers, but I thought I read somewhere that doing 13 % 10, will return the 1 and (13 * 10) % 10 will return the 3 or something like that, but I can't find the post, and I not sure what exactly the '%' operator is doing to get those results?

##### Share on other sites
The modulo operator m%n returns the remainder of the integer division of m by n.

##### Share on other sites
The modulo operator returns the remainder after whole number division:
2 modulo 3 returns 226 modulo 13 returns 09 module 2 returns 1

Integer division discards the remainder, so you can parse a number with a given radix by applying division and modulo:
143 % 10 → 3(143 / 10 → 14) % 10 → 4(14 / 10 → 1) % 10 → 1

That should get you on your way.

##### Share on other sites
thank you you guys.

curious off the tops of your heads, when else could the '%' operator come in handy?

##### Share on other sites
Quote:
 Original post by freeworldthank you you guys.curious off the tops of your heads, when else could the '%' operator come in handy?

Finding out if a number is odd or even is a common use.

##### Share on other sites
The modulus operator can come in handy for hashing key values in a hash table.

It maps a larger range of number to some smaller number of values, between 0 and some N, where N is the size of the hash table. Read up on hash tables for more information on how this works.

##### Share on other sites
Also, if you ever want to 'clamp' an angle to [0-360] or [0-2PI]
540 % 360 = 180fmod (8.343, M_2_PI) = 2.06 // % doesn't work on floating point use fmod()

##### Share on other sites
Quote:
 Original post by freeworldthank you you guys.curious off the tops of your heads, when else could the '%' operator come in handy?

It's used extensively in cryptography, especially public or asymmetric key encryption. You can use the mod operator to set max and min values of a function (rand() for example) cause the maximum value the mod operator will produce for a given value is 1 minus the divisor.

##### Share on other sites
Quote:
 Original post by freeworldthank you you guys.curious off the tops of your heads, when else could the '%' operator come in handy?

It's used extensively in cryptography, especially public or asymmetric key encryption. You can use the mod operator to set max and min values of a function (rand() for example) cause the maximum value the mod operator will produce for a given value is 1 minus the divisor.

##### Share on other sites
Quote:
 curious off the tops of your heads, when else could the '%' operator come in handy?

When using rand() (don't forget to seed the random number generator using srand!) to generate a random number, you can use the modulus operator to return a number between 1 and a maximum.

For example, rand() by itself will return a random number governed by whatever RAND_MAX has been defined as (found in stdlib.h). Using rand()%1000 however, would return a random number between 1 and 1000.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632145
• Total Posts
3004381

×