This is giving me a headache

Does anybody know the codes for this problem? or does anybody encountered this type of problem?
(Check Digit. Credit cards usually have a so-called check digit. This is a single digit that is assigned when
the account number is developed and has a special property. One particularly simple mechanism is to
assign the last digit of the sum of all the other digits. For example, suppose we have a nine-digit account
number (including the check digit ). The check digit would be the sum of the eight digits. This digit could
be placed anywhere in the sequence, say the third digit. As a full example, suppose the eight numbers
are 12345678. Their sum is 36; thus, 6 is the check digit. The account number is therefore 126345678.
Write a program to read in a nine digit integer from the keyboard and check it for these rules. Write
“account number is okay” or “account number is not okay” for the results of the test.)
Do you know "extract" the 1 out of the integer 123?
Do you know how to extract the 3 out of it?
What about the 2?

Hints:
123 % 10 == 3
123 / 10 = 12
there are 2 ways to do this. you can get the input as a string and chop it up and convert the chunks into integers (or whatever else), or you can do it with math.

often one will read as strings to do basic error checking (did the user put in abcdef instead of 123456?) anyway, so that method has additional merits.

the math approach for a different digit:
(123/10) %10 = 12 %10 = 2.

the most and least significant digit in base 10 are easy, the middle ones require a little additional work. It takes a while to get used to tricks with % but once you do, its very useful.

Topic archived. No new replies allowed.