0% found this document useful (0 votes)
9 views4 pages

Lab Task 10

The document outlines a lab task focused on implementing a credit card number validation system using Luhn's Algorithm. It details the criteria for card type identification, the steps involved in Luhn's Algorithm, and provides a Python implementation for validating credit card numbers. The conclusion emphasizes the successful demonstration of the algorithm in ensuring both format correctness and checksum accuracy.

Uploaded by

Muxammil Raheem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Lab Task 10

The document outlines a lab task focused on implementing a credit card number validation system using Luhn's Algorithm. It details the criteria for card type identification, the steps involved in Luhn's Algorithm, and provides a Python implementation for validating credit card numbers. The conclusion emphasizes the successful demonstration of the algorithm in ensuring both format correctness and checksum accuracy.

Uploaded by

Muxammil Raheem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

⮚ Lab Task 10 Report: Credit Card Number

Validation using Luhn's Algorithm

● Objective:

To implement a credit card number validation system using Luhn's Algorithm and
determine whether a credit card number is valid or invalid.

● Card Type Identification:

The credit card number must:

- Be between 13 and 16 digits.


- Start with:
- 4 for Visa
- 5 for MasterCard
- 37 for American Express
- 6 for Discover

● Luhn’s Algorithm Steps:

1. Double every second digit from right to left. If the result is a two-digit number, add
the two digits together.

1
2. Add all the single-digit results from step 1.
3. Add the digits in the odd positions (from right to left).
4. Sum the results from steps 2 and 3.
5. If the total is divisible by 10, the card number is valid; otherwise, it is invalid.

● Python Implementation:

# Return true if the card number is valid


def isValid(number):
return (sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0 and
(prefixMatched(number, 4) or prefixMatched(number, 5) or
prefixMatched(number, 37) or prefixMatched(number, 6)) and 13 <=
getSize(number) <= 16

# Get the result from Step 2


def sumOfDoubleEvenPlace(number):
sum = 0
number = str(number)[::-1]
for i in range(1, len(number), 2):
sum += getDigit(int(number[i]) * 2)
return sum

# Return this number if it is a single digit, otherwise return the sum of the two digits
def getDigit(number):
return number if number < 10 else (number // 10 + number % 10)

# Return sum of odd-place digits in number


def sumOfOddPlace(number):
sum = 0
number = str(number)[::-1]
for i in range(0, len(number), 2):
sum += int(number[i])
return sum

# Return true if the digit d is a prefix for number


def prefixMatched(number, d):
return getPrefix(number, getSize(d)) == d

# Return the number of digits in d


def getSize(d):
return len(str(d))

# Return the first k number of digits from number


def getPrefix(number, k):

2
return int(str(number)[:k])

# Main function to prompt the user


def main():
number = int(input("Enter a credit card number: "))
if isValid(number):
print("The credit card number is valid.")
else:
print("The credit card number is invalid.")

main()

● Flowchart:

(Flowchart image shown below)

● Conclusion:

3
This lab task successfully demonstrates the implementation of Luhn’s Algorithm to
verify credit card numbers. The validation logic ensures both format correctness and
checksum accuracy for common credit card types.

You might also like