CS13002 Programming and Data Structures

Section: 5/E, Spring 2006

Assignment 1

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.

Example

Card Number 5 987654 02468135 5
Step 1 10 91671258 0441282310 5
Step 2 1+0=1 91+6=771+2=358 0441+2=38231+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:

Sample output

   Enter 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


[Submission site] [Lab home] [Course home]