C Language Implementation Luhn check

Source: Internet
Author: User

The Luhn algorithm is mainly used to calculate the legitimacy of credit card numbers. 1, starting from the last digit of the card number, the even digit is multiplied by 2, if the result multiplied by 2 is a double digit, the number of two digits is added to save. 2, add all the numbers, get the sum. 3. If the credit card number is legal, the sum can be divisible by 10


#include <stdio.h> #include <string.h> #include <stdlib.h>int main (int argc, const char * argv[]) {        Char num[30];        while (gets (num)!=null) {            int len= (int) strlen (num);        int sum=0;        int tmp;            for (int i=len-1;i>=0;i--)        {            if ((len-i)%2==1)    //odd digit                sum+=num[i]-' 0 ';            else                //even digit            {               tmp= (num[i]-' 0 ')                * *; sum+=tmp>=10?tmp-9:tmp;            }        }                printf ("sum=%d\n", sum);    }       return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C Language Implementation Luhn check

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.