Credit is a program written in C that validates credit card numbers using Luhn's Algorithm. The program determines whether a card number is valid and identifies the type of card (American Express, MasterCard, Visa, or INVALID) based on card number structure and checksum verification.
Credit card numbers are long decimal numbers that include a checksum to verify their validity. Each type of card has unique properties:
- American Express (AMEX): 15 digits, starting with
34or37. - MasterCard: 16 digits, starting with
51,52,53,54, or55. - Visa: 13 or 16 digits, starting with
4.
The program uses Luhn's Algorithm to validate the card number:
- Multiply every second-to-last digit by 2.
- Add the digits of the resulting products together.
- Add this sum to the sum of the digits that weren’t multiplied by 2.
- If the total modulo 10 is 0, the card number is valid.
- The program prompts the user for a credit card number.
- It validates the card using Luhn’s Algorithm.
- If valid, it identifies the type of card:
AMEXfor American Express.MASTERCARDfor MasterCard.VISAfor Visa.
- If invalid, it outputs
INVALID.
This program requires the CS50 library. Follow these instructions to install it:
curl -s https://packagecloud.io/install/repositories/cs50/repo/script.deb.sh | sudo bash
sudo apt install libcs50curl -s https://packagecloud.io/install/repositories/cs50/repo/script.rpm.sh | sudo bash
sudo dnf install libcs50- Download the latest release from CS50 Library Releases.
- Extract the downloaded file:
tar -xvf libcs50-*.tar.gz cd libcs50-* sudo make install
Use the following command to compile the program with the CS50 library:
gcc -o credit credit.c -lcs50Run the program using:
./creditInput:
Number: 4003600000000014Output:
VISAInput:
Number: 123456789012345Output:
INVALID- Input:
378282246310005 - Output:
AMEX
- Input:
5105105105105100 - Output:
MASTERCARD
- Input:
4111111111111 - Output:
VISA
- Input:
6176292929 - Output:
INVALID
-
Input Validation:
- Prompts the user for a numeric input using
get_longfrom the CS50 library to handle large numbers.
- Prompts the user for a numeric input using
-
Checksum Calculation:
- Implements Luhn’s Algorithm:
- Multiplies every second-to-last digit by 2.
- Sums the digits of the products and the remaining digits.
- Checks if the total modulo 10 equals 0.
- Implements Luhn’s Algorithm:
-
Card Type Identification:
- Identifies the card type based on the length and starting digits:
AMEXfor 15 digits starting with34or37.MASTERCARDfor 16 digits starting with51–55.VISAfor 13 or 16 digits starting with4.
- Identifies the card type based on the length and starting digits:
-
Output:
- Prints the card type or
INVALID.
- Prints the card type or
-
Invalid Length:
- Rejects card numbers with lengths other than 13, 15, or 16.
-
Invalid Checksum:
- Outputs
INVALIDif the checksum fails.
- Outputs
-
Unsupported Starting Digits:
- Outputs
INVALIDfor valid-length numbers that don’t match known card types.
- Outputs
-
Non-Numeric Input:
- The program ensures only numeric input is processed.
This project was developed as part of a CS50 assignment and adheres to its guidelines.