# Validate Credit Card Number [6 kyu] [Validate Credit Card Number](https://www.codewars.com/kata/5418a1dd6d8216e18a0012b2) 6 kyu ## Solution ```cpp= /** *** Author: R-CO *** E-Mail: daniel1820kobe@gmail.com *** Date: 2020-08-05 **/ #include <cctype> #include <cstdlib> #include <iostream> using std::cout; using std::endl; #include <vector> struct TestCaseStruct { int input; bool expected_output; }; class Kata { public: static bool validate(long long int n) { auto digits = SplitToDigits(n); for (size_t index = 1; index < digits.size(); index += 2) { digits[index] <<= 1; if (digits[index] > 9) { digits[index] -= 9; } } int sum = 0; for (const auto digit : digits) { sum += digit; } if (sum % 10 != 0) { return false; } return true; } private: static std::vector<int> SplitToDigits(long long int n) { int base = 1; std::vector<int> digits; do { base *= 10; digits.push_back(n % 10); n /= 10; } while (n > 0); return digits; } }; int main(int argc, char *argv[]) { /* */ TestCaseStruct test_cases[] = {{891, false}, {2121, true}}; Kata solution; for (const auto &test_case : test_cases) { if (solution.validate(test_case.input) == test_case.expected_output) { cout << "test case \"" << test_case.input << "\" is pass." << std::endl; } else { cout << "test case \"" << test_case.input << "\" is fail." << std::endl; } } return EXIT_SUCCESS; } ``` ## Result  ###### tags: `CodeWars` `C++`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up