credit card number check

Im not sure if this would completely qualify as a beginners type question, but this is the first course ive taken of this kind so here i am..here's the assignment i have...i know i have to use a while loop and the mod(%) function but im having trouble coming up with the logic/structure for the code..if anyone could help me it would be great


Credit Card Number Check - The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or swiching digits. The following method is used to verify actual credit card numbers, but, for simplicity, we will describe it for numbers with 8 digits instead of 16:

Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 43589795, then you form the sum 5 + 7 + 8 + 3 = 23.
Double each of the digits that were not included in the preceding step. Add all the digits of the resulting numbers. For example, with the number given above, doubling digits, starting with the next-to-last one, yields 18 18 10 8. Adding all digits in these values yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.
Add the sums of the two preceding steps. If the last digit of the result is zero, the number is valid. In our case, 23 + 27 = 50, so the number is valid.
Write a program that implements this algorithm. The user should supply an 8-digit number, and you should print out whether the number is valid or not. If it is not valid, you should print out the value of the check digit that would make the number valid.

This problem was created by Cay Horstmann from San Jose State University.

Output sample:
Note: The bold letters means the computer displays the output. The normal letters means you enter the input.

Enter the credit card number (8 digits)
43589795
The credit card number is valid.

Enter the credit card number (8 digits)
43589799
The credit card number is invalid.
The last digit should be 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
get the input as char array (not int)

get input length and store it as digit

set sum = 0

loop i from digit-1 to 0 (inclusive)
   determine (digit-1-i) is odd or even
   convert input[i] to int and store it in val
   if odd:
      Double val and adding all digits of val, put to val again
   add val to sum

if sum % 10 == 0:
   print valid
else:
   get the right checkDigit
   print invalid and the right checkDigit
Last edited on
Topic archived. No new replies allowed.