Creditcard Checker :- C Programming (CS50 Project 1)
Credit Card Checker in C
-- CS50 Introduction to Computer Science Project 1 Implementation
Project 1 :-
This project implements a credit card number validator in C, fulfilling the requirements for CS50's Problem Set 1.The solution validates credit card numbers using Luhn's algorithm and identifies the card issuer based on industry-standard patterns.
Project Requirements
- Validate credit card numbers using Luhn's algorithm
- Identify card issuer (Visa, Mastercard, American Express)
- Handle Different card lengths ( 13, 14, 16)
- Return Valid/Invalid
Step 1: Prompting user for input
We start by prompting the user to enter a positive credit card number using get_long function from the cs50 library, using long to accept long digit card numbers.
example.c
// Prompt user for credit card number
long number;
do
{
number = get_long("Number: ");
}
while (number < 1);
Step 2: Counting the length of the Number
We need to count the number of digit to identify card issuer
- American Express - 15 digits and start with 34 or 37
- MasterCard - 16 digits and start with 51-55
- Visa - 13 or 16 digits and start with 4
We done this task by user defined function length_count in which we use loop and each time card number divided by 10 for decrease digits to 0 and each digit decreament added to length counter so at the and we get length of card number.
example.c
// Count number of digits
int length_count(long number)
{
int length = 0;
while (number > 0)
{
number /= 10; // Remove last digit
length++; // Increment count
}
return length;
}
Step 3: Validating With Luhn's Algorithm
Algorithm Steps
- Starting from second last digit, double every second digit.
- Add the digits of each product together.
- Add Step_2 sum to the sum of the digits that weren't multiplied by 2.
- If the total's last digit is 0 means divisible by 10 then number is valid
example.c
// Validate with Luhn's algorithm
char check_valid(long number)
{
int sum1 = 0; // Sum of digits not multiplied
int sum2 = 0; // Sum of digits from multiplied numbers
int multiply = 0;
while (number > 0)
{
// Add last digit to sum1
sum1 += number % 10;
number /= 10;
// Multiply next digit by 2
multiply = number % 10;
multiply *= 2;
// Add digits of product to sum2
while (multiply > 0)
{
sum2 += multiply % 10;
multiply /= 10;
}
number /= 10;
}
// Check if total ends with 0
int total = sum1 + sum2;
return (total % 10 == 0) ? 'Y' : 'N';
}
Step 4: Identifying Card Type
After Luhn's algorithm we can conclude it can be a credit card number but for sure we need to check specific card digits and its starting digits.
In this user defined card_type function we check card number is satisfying any one card specifications then return Valid otherwise return Invalid.
example.c
// Identify card issuer
string card_type(long number, int length)
{
int prefix;
// Check American Express (15 digits)
if (length == 15)
{
prefix = number / 10000000000000; // First two digits
if (prefix == 34 || prefix == 37)
return "AMEX";
}
// Check 16-digit cards
else if (length == 16)
{
prefix = number / 100000000000000; // First two digits
// MasterCard (51-55)
if (prefix >= 51 && prefix <= 55)
return "MASTERCARD";
// Visa (starts with 4)
if (prefix / 10 == 4)
return "VISA";
}
// Check Visa (13 digits)
else if (length == 13)
{
prefix = number / 1000000000000; // First digit
if (prefix == 4)
return "VISA";
}
return "INVALID";
}
Full Code
Here is full code of this project, how we combine all functions made by us in above steps.
example.c
#include <stdio.h>
#include <cs50.h>
// Function prototypes
int length_count(long number);
char check_valid(long number);
string card_type(long number, int length);
int main(void)
{
// Prompt user for credit card number
long number;
do
{
number = get_long("Number: ");
}
while (number < 1);
// Calculate number length
int length = length_count(number);
// Validate using Luhn's algorithm
char valid = check_valid(number);
// Output result
if (valid == 'Y')
{
string card = card_type(number, length);
printf("%s\n", card);
}
else
{
printf("INVALID\n");
}
}
// Count number of digits
int length_count(long number)
{
int length = 0;
while (number > 0)
{
number /= 10;
length++;
}
return length;
}
// Validate with Luhn's algorithm
char check_valid(long number)
{
int sum1 = 0; // Sum of digits not multiplied
int sum2 = 0; // Sum of digits from multiplied numbers
int multiply = 0;
while (number > 0)
{
sum1 += number % 10;
number /= 10;
multiply = number % 10;
multiply *= 2;
while (multiply > 0)
{
sum2 += multiply % 10;
multiply /= 10;
}
number /= 10;
}
int total = sum1 + sum2;
return (total % 10 == 0) ? 'Y' : 'N';
}
// Identify card issuer
string card_type(long number, int length)
{
int prefix;
if (length == 15)
{
prefix = number / 10000000000000;
if (prefix == 34 || prefix == 37)
return "AMEX";
}
else if (length == 16)
{
prefix = number / 100000000000000;
if (prefix >= 51 && prefix <= 55)
return "MASTERCARD";
if (prefix / 10 == 4)
return "VISA";
}
else if (length == 13)
{
prefix = number / 1000000000000;
if (prefix == 4)
return "VISA";
}
return "INVALID";
}
Example
Input: 4111111111111111
Output: Visa
Input: 4111111111111112
Output: Invalid
Try It Yourself: Credit Card Checker
Enter a credit card number below to test whether it's valid using Luhn's Algorithm and see the card type (This tool avoids signs in card number).
Credit Card Checker
Credit Card Checker in C
