Last active
June 23, 2023 15:54
-
-
Save KristineNessa/5499031 to your computer and use it in GitHub Desktop.
The Java code below is an example of how we can validate the "check digit" of a UPC (Universal Product Code) 12 digit code. There are multiple ways the Luhn algorithm can be implemented using code. This Gist is designed to get the thought process rolling and to see how others would modify the code to be more efficient for the same 12 digit UPC "…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Validate a UPC code by calculating for the check digit number. | |
| */ | |
| import java.util.Scanner; | |
| public class ValidateUPCCode { | |
| // Main function | |
| public static void main(String[] args) { | |
| final long LARGEST_POSSIBLE_UPC_CODE = 999999999999L; | |
| // Read UPC Code | |
| Scanner inputUPCCodeStr = new Scanner(System.in); | |
| System.out.print("Enter a 12-digit UPC code: "); | |
| long inputUPCCode = inputUPCCodeStr.nextLong(); | |
| long codeValue = inputUPCCode; | |
| // Check UPC code size and validate UPC code against the check digit number. | |
| if ((codeValue > 0) && (codeValue < LARGEST_POSSIBLE_UPC_CODE)) { | |
| // Get the individual digits of the UPC code, in reverse order | |
| int n12 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n11 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n10 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n9 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n8 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n7 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n6 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n5 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n4 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n3 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n2 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| int n1 = (int) (codeValue % 10); | |
| codeValue /= 10; | |
| // Get sum of the first 5 odd digit numbers | |
| int oddSum = n1 + n3 + n5 + n7 + n9 + n11; | |
| // Get sum of first 5 even digit numbers | |
| int evenSum = n2 + n4 + n6 + n8 + n10; | |
| // Multiply the oddSum value by 3 and add to the evenSum value. Subtract the remainder | |
| // of result/10 (result modulo 10) from 10 to get the calculated check digit | |
| int calcCheckDigit = 10 - ((evenSum + 3 * oddSum) % 10); | |
| if (calcCheckDigit == n12) { | |
| System.out.println(inputUPCCode + " is a valid UPC code"); | |
| } else { | |
| System.out.println(inputUPCCode + " is not a valid UPC code"); | |
| } | |
| } else { | |
| System.out.println(inputUPCCode + "is not a valid UPC code"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code fails for all UPC codes with a check digit of 0.
For example 190198160980 is not accepted, since the calcCheckDigit results in a value of 10 (instead of 0)
Changing line 56 solves that problem:
int calcCheckDigit = (10 - ((evenSum + 3 * oddSum) % 10)) % 10;