views:

73

answers:

4

This isn't actually homework, I'm just looking through some questions in a discrete maths book before I start computer science next week.

Anyway, one of the questions asks me to write a program to perform this algorithm (which it explains). The part I'm stuck with is how to take the 9 digit number and "split" it into single integers, so the calculations can be performed on each digit.

I thought of dividing the number by 100,000,000 and then taking the integer value of this to get the first digit, but I'm not sure how to get the others.

If this was in PHP or something I could just use explode(), but I guess that's not the point here :P

+2  A: 

You can use the mod(%) and divide(/) operator.

N%10 would give you the last digit. N/10 (integer division) will remove the last digit.

You can continue till you have no more digits.

Gunner
A: 

use the modulo operation:

a % 10 to get the last digit
a % 100 to get the last two digits. (a % 100) - (a % 10) to get the second last number
etc.
knittl
+1  A: 

Once you've divided by 100,000,000 and taken the integer value, you can then multiply this integer value back by 100,000,000 and subtract it from the ISBN. That effectively just takes off the left-most digit. So now, repeat with 10,000,000 - and so on.

Example with 5 digits:

Start: 74325

74325/10000 and int = 7 (there's your first digit)
7 * 10000 = 70000
74325 - 70000 = 4325

4325/1000 and int = 4 (there's your next digit)
4 * 1000 = 4000
4325 - 4000 = 325

and so on!

Spacedman