Created
March 14, 2022 05:22
-
-
Save mattnicee7/4b2c8e2012adac4ad65479e8c6c21065 to your computer and use it in GitHub Desktop.
Esse código adivinha os ultimos 2 digitos do seu cpf.
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
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