Skip to content

Instantly share code, notes, and snippets.

@parpok
Created September 6, 2024 21:42
Show Gist options
  • Save parpok/e986ddb05670bfc4e878a1174b2cd6d3 to your computer and use it in GitHub Desktop.
Save parpok/e986ddb05670bfc4e878a1174b2cd6d3 to your computer and use it in GitHub Desktop.

Revisions

  1. parpok created this gist Sep 6, 2024.
    48 changes: 48 additions & 0 deletions CardValidateAlgorithm.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@

    #include <iostream>

    int main()
    {
    std::string CardNumba;

    int CardNumbers[16];
    int SecondCardNumbers[8];

    int fullCardEq{};

    std::cout << "Gib card numba\n";
    std::cin >> CardNumba;

    if (CardNumba.length() != 16) {
    std::cout << "Card number must be 16 digits long!\n";
    return 1;
    }


    for (int i = 0; i < CardNumba.length(); i++) {
    CardNumbers[i] = CardNumba[i] - '0';
    }

    for (int i = 0; i < 16; i++) {
    int currentDigit = CardNumbers[i];

    if (i % 2 == 0) {
    currentDigit *= 2;
    if (currentDigit > 9) {
    currentDigit -= 9;
    }

    }

    fullCardEq += currentDigit;
    }

    if (fullCardEq % 10 == 0) {
    std::cout << "Valid card \n";
    }
    else {
    std::cout << "Bad card \n";
    }

    return 0;
    }