Giter Site home page Giter Site logo

the-luhn-algorithm's Introduction

Luhn Algorithm for Credit Card Validation

This is a Python & C implementation of the Luhn Algorithm, a simple checksum formula used to validate credit card numbers.

Requirements

To run this code, you will need:

  • Python 3.x and maybe C

Usage

To use the Luhn Algorithm, simply call the get_credit_card_number() function and pass in the credit card number as a string:

def get_credit_card_number():
    while True:
        input_str = input("Enter the credit card number: ")  #
        if not input_str.isdigit():
            print("Invalid input. Please enter a positive integer.")               
            continue
        user_input = int(input_str)
        if user_input <= 0:
            print("Invalid input. Please enter a positive integer.")
            continue
        return user_input 

How it works

The Luhn Algorithm works by summing up the digits of the credit card number, starting from the rightmost digit and working backwards. For every other digit, starting with the second-to-last digit, the digit is doubled and the sum of the digits of the resulting number is added to the total sum. Finally, the total sum is divided by 10, and the credit card number is valid if the remainder is 0. For example, consider the credit card number 4111 1111 1111 1111. Starting from the rightmost digit (which is 1), we add it to the total sum (which is currently 0). Moving to the next digit (which is 1 again), we double it to get 2, and then add the digits of the resulting number (2) to the total sum (which is now 2). Continuing in this manner, we eventually get a total sum of 16. Since 16 is not divisible by 10, the credit card number is not valid.

Testing

To run the unit tests, simply run the following command:

python creditcard.py

This will run a series of tests to verify that the get_credit_card_number() function correctly identifies valid and invalid credit card numbers.

Contributing

If you find a bug or have a suggestion for how to improve this code, please feel free to open an issue or submit a pull request.

the-luhn-algorithm's People

Contributors

dudeiebot avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.