Credit card numbers follow certain patterns. A credit card number must have between 13 and 19 digits. It must start with for example:
4 for Visa cards
5 for Master cards
37 for American Express cards
6 for Discover cards
So, this here is the code thats identify either a credit card valid or not by applying the Luhn's Algorithm.
But i need help on how to extend my code to identify the type of credit card after the user has inputted the credit card number.
The type of credit card can be identified by using the example from above like every credit card number for Visa starts with a 4.
I suppose i should use arrays but im kinda confused right now. I hope someone can help me thank you :)
#include <stdio.h>
#include <string.h>
int luhn(const char* cc) {
const int m[]= { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
int i, odd= 1, sum= 0;
for ( i= strlen(cc); i--; odd= !odd ) {
int digit= cc[i] - '0';
sum += odd ? digit : m[digit];
}
return sum % 10 == 0;
}
int main( void ) {
char buf[200];
printf( "Enter number: " );
scanf( "%s", buf );
printf( "%s\n", luhn( buf )? "Valid" : "Invalid" );
system("PAUSE");
return 0;
}