CS13002 Programming and Data Structures
Section: 5/E, Spring 2006
In this assignment you are asked to write a program that checks whether a credit card number is valid. Let us first look into the structure of a credit card number. Let us assume 16-digit credit card numbers. The first (leftmost) digit corresponds to the type of the card. For example, 4 stands for VISA cards, 5 stands for MASTERCARDs, 3 stands for Diner's Club and American Express cards, etc. The next 6 digits correspond to the number of the bank that issues the card. The next 8 digits constitute the account number of the card-holder. The last (rightmost) digit is a check digit that determines whether the credit card number is valid or not.
The check digit (in a valid card) is calculated in such a way that the checksum corresponding to the card number is divisible by 10 (ten). In order to compute the checksum, we first add the digits at the even positions (i.e., the second, fourth, sixth, ..., sixteenth digits) of the card number. Next each digit in an odd position is doubled. If the doubled value exceeds 9, then the two digits are added. Finally, these doubled digits (after adjustment of 2-digit values) are added to the checksum. The card number is taken to be valid if and only if the checksum is a multiple of 10.
Card Number 5 9 8 7 6 5 4 0 2 4 6 8 1 3 5 5 Step 1 10 9 16 7 12 5 8 0 4 4 12 8 2 3 10 5 Step 2 1+0=1 9 1+6=7 7 1+2=3 5 8 0 4 4 1+2=3 8 2 3 1+0=1 5 Checksum = 1+9+7+7+3+5+8+0+4+4+3+8+2+3+1+5 = 70
Since the checksum is divisible by ten, 5987 6540 2468 1355 is a valid credit card number.
Write a program that does the following:
- Read a 16-digit credit card number from the user. Since a 16-digit integer cannot be accommodated in a long int, read the input as a string (a character array).
- Compute the checksum for the number provided by the user.
- Determine whether the number is valid by computing the remainder of division of the checksum by ten.
Sample outputEnter a 16-digit credit card number : 5987654024681355 Checksum = 70 This is a valid credit card number. Enter a 16-digit credit card number : 4321098667788990 Checksum = 86 This is an invalid credit card number.
Report the output of your program on the following four numbers:5235711131719230 4142857888888884 3139872193769783 2345678901234567