Skip to content

Instantly share code, notes, and snippets.

@Welding-Torch
Created July 21, 2023 16:49
Show Gist options
  • Save Welding-Torch/3dd57afb5878d565669d8c58d35e2401 to your computer and use it in GitHub Desktop.
Save Welding-Torch/3dd57afb5878d565669d8c58d35e2401 to your computer and use it in GitHub Desktop.

Revisions

  1. Welding-Torch revised this gist Jul 21, 2023. 1 changed file with 146 additions and 1 deletion.
    147 changes: 146 additions & 1 deletion credit.c
    Original file line number Diff line number Diff line change
    @@ -1 +1,146 @@
    ‎‎​
    #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";
    }

    }
  2. Welding-Torch created this gist Jul 21, 2023.
    1 change: 1 addition & 0 deletions credit.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎​