Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Created April 3, 2012 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertoMonteiro/2291088 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/2291088 to your computer and use it in GitHub Desktop.
Gerador de CPF
using System;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
[STAThread]
static void Main(string[] args)
{
var r = new Random();
int a1 = r.Next(10), a2 = r.Next(10), a3 = r.Next(10),
a4 = r.Next(10), a5 = r.Next(10), a6 = r.Next(10),
a7 = r.Next(10), a8 = r.Next(10), a9 = r.Next(10);
int b1 = a1 * 10,
b2 = a2 * 9,
b3 = a3 * 8,
b4 = a4 * 7,
b5 = a5 * 6,
b6 = a6 * 5,
b7 = a7 * 4,
b8 = a8 * 3,
b9 = a9 * 2;
int dv1 = b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9;
int t2 = dv1 % 11;// Cálculo do resto da divisão
int primeiroDigitoVerificador;
if (t2 <= 2)
primeiroDigitoVerificador = 0;
else
primeiroDigitoVerificador = 11 - t2;
int c1 = a1 * 11,
c2 = a2 * 10,
c3 = a3 * 9,
c4 = a4 * 8,
c5 = a5 * 7,
c6 = a6 * 6,
c7 = a7 * 5,
c8 = a8 * 4,
c9 = a9 * 3,
c10 = primeiroDigitoVerificador * 2;
int dv2 = c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10;
int t4 = dv2 % 11; // Cálculo do resto da divisão
int segundoDigitoVerificador;
if (t4 <= 2)
segundoDigitoVerificador = 0;
else
segundoDigitoVerificador = 11 - t4;
var v = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}", a1, a2, a3, a4, a5, a6, a7, a8, a9, primeiroDigitoVerificador, segundoDigitoVerificador);
Clipboard.SetText(v);
Console.WriteLine("O CPF Gerado foi: {0}{1}{2}.{3}{4}{5}.{6}{7}{8}-{9}{10}", a1, a2, a3, a4, a5, a6, a7, a8, a9, primeiroDigitoVerificador, segundoDigitoVerificador);
Console.ReadKey(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment