Created
October 21, 2014 04:19
-
-
Save wirasetiawan29/8211c7491015d8083e2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] CreateNumber(int length) | |
{ | |
Random random = new Random(); | |
int[] digits = new int[length]; | |
// For loop keeps default value of zero for | |
last slot in array | |
for(int i = 0; i < length - 1; i++) | |
{ | |
digits[i] = random.Next(10); | |
} | |
int sum = 0; | |
bool alt = true; | |
for(int i = length - 2; i >= 0; i--) | |
{ | |
if(alt) | |
{ | |
int temp = digits[i]; | |
temp *= 2; | |
if(temp > 9) | |
{ | |
temp -= 9; | |
} | |
sum += temp; | |
} | |
else | |
{ | |
sum += digits[i]; | |
} | |
alt = !alt; | |
} | |
int modulo = sum % 10; | |
if(modulo > 0) | |
{ | |
digits[length-1] = 10 - modulo; | |
} | |
// No else req'd - keep default value of | |
zero for digits[length-1] | |
return digits; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment