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.
Luhn algorithm - Algorithm used to validate cards - raw dogging loops.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment