Luhn algorithm--verification of credit card numbers

Source: Internet
Author: User

The verification of the credit card number is based on the Luhn algorithm: the old IBM engineer Hans Peter Luhn invented in 1954.

The patent was applied at that time and is now open to the public domain of knowledge as a standard for international standards organizations: ISO/EC 7812-1.

    1. Starting with the last digit of the card number, the inverse adds the odd digit number to the sum
    2. Starting with the last digit of the card number, multiply the even digits by 2, and if the product is a two-digit number, subtract 9 (or two-digit sum) and sum
    3. The sum of odd digits is combined with the sum of even digits, and the result can be divisible by 10.

1 intLuhncheck (vector<int> &digitnums)2 {3     intSZ =digitnums.size ();4     intsum =0;5      for(intI=1; I <= sz; i++)6     {7         if(i%2==0)8         {9             intval = digitnums[sz-i] *2;Ten             if(Val >9) One             { AVal-=9; -             } -Sum + =Val; the         } -         Else -         { -Sum + = digitnums[sz-i]; +         } -     } +cout<<"Check sum:"<<sum<<Endl; A     if(sum%Ten==0) at     { -         return 1; -          } -      -}return 0;
View Code

Luhn algorithm--verification of credit card numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.