How to resolve the algorithm Luhn test of credit card numbers step by step in the Kotlin programming language
How to resolve the algorithm Luhn test of credit card numbers step by step in the Kotlin programming language
Table of Contents
Problem Statement
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:
For example, if the trial number is 49927398716:
Write a function/method/procedure/subroutine that will validate a number with the Luhn test, and use it to validate the following numbers:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Luhn test of credit card numbers step by step in the Kotlin programming language
Explanation:
1. Overview:
This Kotlin program checks the validity of credit card numbers using the Luhn algorithm, which is a checksum formula used to detect errors in numerical data.
2. Function:
checkLuhn(number: String): Boolean
Function Arguments:
number
: The credit card number as a string to be checked.
Function Logic:
isOdd
: A boolean flag used to alternate the calculations for each digit in the credit card number.sum
: An integer to keep track of the total sum of the calculated digits.
The function iterates through the digits of the credit card number from right to left (reversed order). For each digit:
- It converts the character to an integer by subtracting the ASCII value of '0' (
number[index] - '0'
). - If
isOdd
is true, it adds the digit to the sum. - If
isOdd
is false, it doubles the digit, calculates the sum of its two digits, and adds it to the sum. - It alternates
isOdd
to apply the correct calculation (doubling or not) for the next digit.
After iterating through all digits, the function checks if sum % 10
is equal to 0. If it is, the credit card number is considered valid according to the Luhn algorithm. Otherwise, it's considered invalid.
3. Main Function:
main(args: Array<String>)
Main Function Logic:
The main
function is the entry point of the program. It contains an array of credit card numbers (numbers
) to be checked.
- It iterates through each credit card number in the array.
- For each number, it calls the
checkLuhn
function and prints the result ("valid" or "invalid") along with the number.
4. Output:
The program outputs the following:
49927398716 is valid
49927398717 is invalid
1234567812345678 is valid
1234567812345670 is invalid
In this example, the first and third credit card numbers are valid, while the second and fourth are invalid.
Source code in the kotlin programming language
// version 1.0
fun checkLuhn(number: String): Boolean {
var isOdd = true
var sum = 0
for (index in number.indices.reversed()) {
val digit = number[index] - '0'
sum += if (isOdd) digit else (digit * 2).let { (it / 10) + (it % 10) }
isOdd = !isOdd
}
return (sum % 10) == 0
}
fun main(args: Array<String>) {
val numbers = arrayOf("49927398716", "49927398717", "1234567812345678", "1234567812345670")
for (number in numbers)
println("${number.padEnd(16)} is ${if(checkLuhn(number)) "valid" else "invalid"}")
}
You may also check:How to resolve the algorithm Guess the number step by step in the LOLCODE programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the Tcl programming language
You may also check:How to resolve the algorithm Arrays step by step in the HolyC programming language
You may also check:How to resolve the algorithm Call a foreign-language function step by step in the C programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Swift programming language