Validating Credit Cards Credit card numbers are not randomly chosen. There are rules that determine which credit card numbers are valid. This is a limited attempt to prevent fraud. If you just make up some number, it is unlikely to be valid. You are going to write a program to determine if a number is a valid credit card number. A credit card number must have between 13 and 16 digits. The first digit or two deter mines what type of card it is: Begins with Card Type Visa MasterCard 37 American Express Discover For our purposes, a number that doesn\'t begin with one of these sequences is not a valid credit card number. The algorithm for validating credit cards is credited to Hans Luhn of IBM. To illustrate the algorithm, we will use the sample number 4388576018402626. First, we see that this must be a Visa card, since it starts with 4. To validate it 1. Double every second digit from right to left. If doubling of a digit results in a two- digit number, add up the two digits to get a single-digit number. 2 2 4 2 2 4 2 38 1 2 12 2 3) 5 2 8 2 16 (1 6-7) 4 2 2. Now, add all the single digit numbers from the previous step. 4 4 8 2 3 1 7 8 37 3. Add all the digits in the odd places from right to left in the number. 6 6 0 8 0 7 8 3 38 4. Add these two values together. 37 38 75 Solution #include #include #include int main(void) { double cardnumber; printf(\"Give me a number: \ \"); scanf(\"%lf\", &cardnumber); if(cardnumber < 1000000000000 || cardnumber > 10000000000000000) { printf(\"INVALID\ \"); return 0; } if(cardnumber < 100000000000000 && cardnumber > 9999999999999) { printf(\"INVALID\ \"); return 0; } char creditcard[17]; sprintf(creditcard, \"%f\", cardnumber); char* ptr_cc; ptr_cc = strtok(creditcard,\".\"); int card_size = strlen(ptr_cc); int sum = 0; for(int i = 1; i < card_size; i+=2) { int x = creditcard[card_size-1-i] - \'0\'; int prod = 2 * x; if(prod>=10) { prod = prod%10 + prod/prod%10; } sum += prod; } for(int i = 0; i < card_size; i+=2) { int x = creditcard[card_size-1-i] - \'0\'; sum += x; } if(sum%10 != 0) { printf(\"INVALID\ \"); return 0; } else if(creditcard[0] == \'4\') { printf(\"VISA\ \"); return 0; } else if(creditcard[0] == \'3\' && (creditcard[1] == \'7\' || creditcard[1] ==\'4\')) { printf(\"AMEX\ \"); return 0; } else if(creditcard[0] == \'5\' && (creditcard[1] >=\'1\' && creditcard[1] <=\'5\')) { printf(\"MASTERCARD\ \"); return 0; } else { printf(\"INVALID\ \"); return 0; } }.