Advertisement
fabi2295

CPF

May 27th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class CPF {
  4.  
  5.     private static Scanner ler;
  6.  
  7.     public static void main(String[] args) {
  8.         int[] numero = new int[12];
  9.         int soma1 = 0, soma2 = 0,resto1 = 0, resto2 = 0,i;
  10.         ler = new Scanner(System.in);
  11.  
  12.         System.out.println("Digite se CPF(só os números)");
  13.         String CPF = ler.next();
  14.  
  15.         for (i = 0; i < 11; i++) {
  16.             numero[i] = Integer.parseInt((String) CPF.substring(i, i + 1));
  17.         }
  18.  
  19.         for (i = 1; i <= 9; i++) {
  20.             soma1 = soma1 + (numero[i] * (11 - i));
  21.         }
  22.        
  23.         if (soma1 % 11 <= 1) {
  24.             resto1 = 0;
  25.         } else {
  26.             resto1 = 11 - (soma1 % 11);
  27.         }
  28.  
  29.         for (i = 1; i <= 10; i++) {
  30.             soma2 = soma2 + (numero[i] * (12 - i));
  31.         }
  32.  
  33.        
  34.         if (soma2 % 2 <= 1) {
  35.             resto2 = 0;
  36.         } else {
  37.             resto2  = 11 - (soma2%11);
  38.         }
  39.  
  40.         if (resto1 == numero[10] && resto2 == numero[11]) {
  41.             System.out.println("CPF valido : " + CPF);
  42.  
  43.         } else {
  44.             System.out.println("Cresto1 = 0, resto2 = 0,iPF invalido : " + CPF);
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement