Member Avatar for Jenniferting

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;
}

Your algorithm for that function is incorrect. According to this article, you have to double every second digit.

It's not clear from that function whether it is validating the account number or calculating a checkdigit. If you are validating the account number there is no need to calculate the checkdigit because the checkdigit is the last digit of the account number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.