Created
February 18, 2020 17:07
-
-
Save feehe21/e18600308df92552ec4886b728dfbb20 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.*; | |
public class UPCGenerator | |
{ | |
public UPCGenerator(){ | |
Scanner scan = new Scanner(System.in); | |
String hold = scan.next(); | |
if(hold.length() == 11){ | |
System.out.println(UPC(hold) + ""); | |
} | |
} | |
public int UPC(String n){ | |
int oddsSum = 0; | |
int evensSum = 0; | |
String hold = ""; | |
for(int i = 0; i < 11; i+=2){ | |
hold = n.substring(i, i+1); | |
oddsSum += Integer.parseInt(hold); | |
} | |
oddsSum *= 3; | |
for(int i = 1; i < 11; i+=2){ | |
hold = n.substring(i, i+1); | |
evensSum += Integer.parseInt(hold); | |
} | |
int totalSum = oddsSum + evensSum; | |
if((totalSum%10) == 0){ | |
return 0; | |
} | |
return 10-(totalSum%10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment