Skip to content

Instantly share code, notes, and snippets.

@georgepaoli
Created October 19, 2018 01:07
Show Gist options
  • Save georgepaoli/d87139dc7e3782e07066bc260f539d7d to your computer and use it in GitHub Desktop.
Save georgepaoli/d87139dc7e3782e07066bc260f539d7d to your computer and use it in GitHub Desktop.
Gerador de CNPJ em C#
public static String GerarCnpj()
{
int soma = 0, resto = 0;
int[] multiplicador1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
int[] multiplicador2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
Random rnd = new Random();
string semente = rnd.Next(10000000, 99999999).ToString() + "0001";
for (int i = 0; i < 12; i++)
soma += int.Parse(semente[i].ToString()) * multiplicador1[i];
resto = soma % 11;
if (resto < 2)
resto = 0;
else
resto = 11 - resto;
semente = semente + resto;
soma = 0;
for (int i = 0; i < 13; i++)
soma += int.Parse(semente[i].ToString()) * multiplicador2[i];
resto = soma % 11;
if (resto < 2)
resto = 0;
else
resto = 11 - resto;
semente = semente + resto;
return semente;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment