How do I check the validity of the Canadian Social Insurance Number in C#?

Posted by user518307 on Stack Overflow See other posts from Stack Overflow or by user518307
Published on 2010-11-24T04:15:06Z Indexed on 2011/01/01 17:54 UTC
Read the original article Hit count: 288

Filed under:
|
|

I've been given the assignment to write an algorithm in C# that checks the validity of a Canadian Social Insurance Number (SIN). Here are the steps to validate a SIN.

Given an example Number: 123 456 782

  1. Remove the check digit (the last digit): 123456782
  2. Extract the even digits (2,4,6,8th digith): 12345678
  3. Double them:
        2  4  6  8
        |  |  |  |
        v  v  v  v
        4  8  12 16 
    
  4. Add the digits together:
    4+8+1+2+1+6 = 22
  5. Add the Odd placed digits:
        1+3+5+7 = 16
          Total : 38

Validity Algorithm

  1. If the total is a multiple of 10, the check digit should be zero.
  2. Otherwise, Subtract the Total from the next highest multiple of 10 (40 in this case)
  3. The check digit for this SIN must be equal to the difference of the number and the totals from earlier (in this case, 40-38 = 2; check digit is 2, so the number is valid)

I'm lost on how to actually implement this in C#, how do I do this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about algorithm