Skip to content

Instantly share code, notes, and snippets.

@mattnicee7
Created March 14, 2022 05:22
Show Gist options
  • Save mattnicee7/4b2c8e2012adac4ad65479e8c6c21065 to your computer and use it in GitHub Desktop.
Save mattnicee7/4b2c8e2012adac4ad65479e8c6c21065 to your computer and use it in GitHub Desktop.
Esse código adivinha os ultimos 2 digitos do seu cpf.
public class Application {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
System.out.print("Digite os 9 primeiros numeros do seu cpf: ");
List<Integer> cpf = Arrays.stream(input.next().split(""))
.map(Integer::parseInt)
.collect(Collectors.toList());
cpf.add(getVerificationCode(10, cpf));
cpf.add(getVerificationCode(11, cpf));
System.out.println(cpf);
}
}
private static int getVerificationCode(int multiplier, List<Integer> cpf) {
double result = 0.0;
for (Integer code : cpf) {
result += (code * multiplier--);
}
return (result % 11 == 0 || result % 11 == 1) ? 0 : (int) Math.round(11 - (result % 11));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment