DEV Community

VINOTH
VINOTH

Posted on

Credit Card Validation Using Python While Loop – Luhn Algorithm

How to Validate a Credit Card Number Using Python and the Luhn Algorithm?

no = 4532015112830366

total = 0
count = 0

while no > 0:
    digit = no % 10
    no = no // 10


    if count % 2 == 1:
        digit = digit*2


        if digit > 9:
            digit = digit % 10 + digit // 10

    total += digit
    count += 1

if total % 10 == 0:
    print("Valid credit card")    
else:
    print("Invalid credit card")            

Enter fullscreen mode Exit fullscreen mode

o/p:

Valid credit card
Enter fullscreen mode Exit fullscreen mode

Write a Python program to validate a credit card number using the Luhn Algorithm. The program should process the digits from right to left, double every second digit, reduce two-digit results by adding their digits, calculate the total, and determine whether the credit card number is valid.

Top comments (0)