Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created February 18, 2020 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/e18600308df92552ec4886b728dfbb20 to your computer and use it in GitHub Desktop.
Save feehe21/e18600308df92552ec4886b728dfbb20 to your computer and use it in GitHub Desktop.
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