learn

How the CPF and CNPJ check digits are calculated

Step-by-step calculation of the two check digits of Brazil’s CPF and CNPJ, with a worked example and the new alphanumeric CNPJ rule from July 2026.

What the check digits are for

The last two digits of Brazil's CPF (individual taxpayer number) and CNPJ (company number) are not part of the identifier: they are computed from the other digits with a modulo-11 rule. They catch typing mistakes, such as one wrong digit or two swapped digits, before a record reaches the tax authority. If you integrate a system with Brazil, you will validate them in forms and imports. A number with correct check digits is well-formed; it does not mean it exists or belongs to anyone. That is what the CPF validator checks, and test numbers made by the CPF generator or the CNPJ generator pass it by construction.

Computing the CPF check digits

A CPF has 9 base digits and 2 check digits. For the first, multiply the 9 digits by the weights 10, 9, 8… 2, add them up and take the remainder of the division by 11: if the remainder is less than 2 the digit is 0, otherwise it is 11 minus the remainder. For the second, repeat with the weights 11, 10… 2 over the 9 digits plus the first check digit. Example with the base 111.444.777:

Digits111444777sum
Weights (1st)1098765432162
Weights (2nd)11109876543198
  • First digit: 162 ÷ 11 leaves 8, so 11 − 8 = 3.
  • Second digit: 198 + 3 × 2 = 204; 204 ÷ 11 leaves 6, so 11 − 6 = 5.

The full CPF is 111.444.777-35. Repeated digits (111.111.111-11) pass the math but are never issued, and a good validator rejects them.

Computing the CNPJ check digits

A CNPJ has 12 base characters (8 for the company, 4 for the branch) and 2 check digits. The weights are 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 for the first and 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 for the second, with the same remainder rule. For 11.222.333/0001: the sum is 102, remainder 3, first digit 8; with it the second sum is 120, remainder 10, second digit 1. Result: 11.222.333/0001-81.

The alphanumeric CNPJ (July 2026)

Since July 2026 the Receita Federal issues CNPJs whose base may contain letters (Normative Instruction RFB 2,229/2024). Existing numeric CNPJs stay valid, and both check digits remain numeric. The math is the same, replacing each character by its ASCII code minus 48: digits keep their values 0 to 9 and letters become A = 17, B = 18… Z = 42. Example: in 12.ABC.345/01DE the values are 1, 2, 17, 18, 19, 3, 4, 5, 0, 1, 20, 21; the first sum is 459 (remainder 8, digit 3) and the second is 424 (remainder 6, digit 5), giving 12.ABC.345/01DE-35. Systems must accept upper-case letters in the base and store the CNPJ as text, not as a number.

In code

function checkDigit(digits, weights) {
  const sum = digits.reduce((acc, d, i) => acc + d * weights[i], 0);
  const rest = sum % 11;
  return rest < 2 ? 0 : 11 - rest;
}
const base = [1, 1, 1, 4, 4, 4, 7, 7, 7];
const d1 = checkDigit(base, [10, 9, 8, 7, 6, 5, 4, 3, 2]); // 3
const d2 = checkDigit([...base, d1], [11, 10, 9, 8, 7, 6, 5, 4, 3, 2]); // 5

A regular expression can only check the layout (dots and dash), as shown in regex for beginners; only the computation checks the digits.

FAQ

Does a valid CPF mean the person exists?

No. Correct check digits only show the number follows the rule. Whether a CPF exists and is in good standing requires a query to the Receita Federal.

Can I use a generated CPF to sign up somewhere?

No. Generated numbers are for testing software with data that passes validation. Using a number that is not yours as an identity document is a crime in Brazil.

What changes in my system with the alphanumeric CNPJ?

The field must accept letters in the base, validation must convert each character to its ASCII code minus 48, and the CNPJ must be stored as 14 characters of text.

Why do remainders 0 and 1 give the digit 0?

Because 11 − 0 = 11 and 11 − 1 = 10 do not fit in one digit; the modulo-11 rule used by the Receita sets 0 in both cases.