Created
July 21, 2023 16:49
-
-
Save Welding-Torch/3dd57afb5878d565669d8c58d35e2401 to your computer and use it in GitHub Desktop.
My Credit Card Validation Program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
int initialvalidate(long cardnumber); // Checks if cardnumber is 13, 15, or 16 digits long. | |
int validate(long cardnumber); // Luhn's algorithm. | |
string whichbrand(long cardnumber); // Tells which brand i.e. VISA, MASTERCARD, AMEX. | |
int countdigits(long num); // Used for initial validation. | |
int main(void) | |
{ | |
int valid, initialvalidation; | |
string brand; | |
long cardnumber = get_long("Number: "); | |
initialvalidation = initialvalidate(cardnumber); | |
if (initialvalidation == 1) | |
{ | |
valid = validate(cardnumber); // should be 0 or 1 | |
if (valid == 1) | |
{ | |
brand = whichbrand(cardnumber); | |
printf("%s", brand); | |
} | |
else | |
{ | |
printf("INVALID\n"); | |
} | |
} | |
else | |
{ | |
printf("INVALID\n"); | |
} | |
} | |
int countdigits(long num) | |
{ | |
int count = 0; | |
do | |
{ | |
count++; | |
num = num / 10; | |
} | |
while (num != 0); | |
return count; | |
} | |
int initialvalidate(long cardnumber) // check if cardnumber is less than 13 or more than 16 | |
{ | |
if (countdigits(cardnumber) < 13) | |
{ | |
return 0; | |
} | |
else if (countdigits(cardnumber) == 13) | |
{ | |
return 1; | |
} | |
else if (countdigits(cardnumber) == 14) | |
{ | |
return 0; | |
} | |
else if (countdigits(cardnumber) == 15) | |
{ | |
return 1; | |
} | |
else if (countdigits(cardnumber) == 16) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
int validate(long cardnumber) // Luhn’s Algorithm | |
{ | |
int sum = 0, othersum = 0, counter = 1, total = 0, currentnum, currentnumtimes2; | |
long restofcardno; | |
restofcardno = cardnumber; | |
do | |
{ | |
currentnum = restofcardno % 10; | |
if (counter % 2 == 0) // if it is even, do this | |
{ | |
currentnumtimes2 = currentnum * 2; | |
if (currentnumtimes2 > 9) | |
{ | |
sum = sum + currentnumtimes2 / 10 + currentnumtimes2 % 10; | |
} | |
else | |
{ | |
sum = sum + currentnumtimes2; | |
} | |
} | |
else // if it is odd, do this. | |
{ | |
othersum = othersum + currentnum; | |
} | |
counter++; | |
restofcardno = restofcardno / 10; | |
} | |
while (restofcardno != 0); | |
total = sum + othersum; | |
if (total % 10 == 0) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
int getFirst2Digits(long input) | |
{ | |
while (input >= 100) | |
{ | |
input /= 10; | |
} | |
return input; | |
} | |
string whichbrand(long cardnumber) | |
{ | |
int First2 = getFirst2Digits(cardnumber); | |
if (First2 == 34 || First2 == 37) | |
{ | |
return "AMEX\n"; | |
} | |
else if (First2 == 51 || First2 == 52 || First2 == 53 || First2 == 54 || First2 == 55) | |
{ | |
return "MASTERCARD\n"; | |
} | |
else if (First2 / 10 == 4) | |
{ | |
return "VISA\n"; | |
} | |
else | |
{ | |
return "INVALID\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment