The following algorithm is for checking a person's birth date / birth number combination (an equivalent to social security number).
The birth date / birth number combination consists of a 10 digit number, YYMMDDNNNN, for example 7401204933.
To decide if this number is correct, you multiply each digit alternatively with 2 and 1, starting with 2.
Next, you add the results together. Finally, you have to be able to evenly divide the sum by 10, for the number to be correct.
For example, checking the number 7401204933 would look like this:
7 * 2 = 14 ---> 1 + 4
4 * 1 = 4 ---> 4
0 * 2 = 0 ---> 0
1 * 1 = 1 ---> 1
2 * 2 = 4 ---> 4
0 * 1 = 0 ---> 0
4 * 2 = 8 ---> 8
9 * 1 = 9 ---> 9
3 * 2 = 6 ---> 6
3 * 1 = 3 ---> 3
Sum it up: 1 + 4 + 4 + 0 + 1 + 4 + 0 + 8 + 9 + 6 + 3 = 40
And then divide by 10: 40 / 10 = 4 (this tells you that the number is correct)
And now for my question: how would one translate such an algorithm into C# code, considering that the program will read the birth date / birth number combination as a string I assume that I first have to transform the string into a numeric value
The sole purpose of this little program is to check if any given birth date / birth number combination is correct.
The code would start off something like this:
using System;
public class TryNumber
{
public static void Main()
{
string number;
Console.Write("Your birth date and birth number (YYMMDDNNNN) ");
number = Console.ReadLine();
// but then what..
Any help would be much appreciated.
Best regards,
Magnus Eklund

turn this algorithm into code...help please
Univer Blue
public class TryNumber
{
public static void Main()
{
string number;
Console.Write("Your birth date and birth number (YYMMDDNNNN) ");
number = Console.ReadLine();
if (IsValidCode(number))
{
// Processing if valid.
}
else
{
// Error message or whatever
}
baka_deshi
THANKS Matthew! It's working!
Really appreciate this! Have a nice weekend
Best regards,
Magnus Eklund
MarkusEilers
its using a bit of "magic" from the ASCII set.
if you look here http://www.lookuptables.com/ you will see numbers go from 0x30 (0) to 0x39 (9)
because of this I can take any number .. convert it to its ascii code, and subtract the ascii code of the 0 character (0x30) from it to receive number representing its value.
try 7
7(0x37) - 0 (0x30) = 7
Cheers,
Greg
JP 360
hi,
thx mathew i would never thing to do it this way, really its very nice but i missed one that i didn't understand it
int digit = c - '0';what does it mean and how it works, when i removed -'0' it didn't work and -'0' actualy do nothing to the value
would you plz enlighten me about this
thx
Loane Sharp
using System;
public class TryNumber
{
public static void Main()
{
string number;
Console.Write("Your birth date and birth number (YYMMDDNNNN) ");
number = Console.ReadLine();
if (IsValidCode(number))
{
Console.WriteLine("The number is correct."); // Processing if valid.
}
else
{
Console.WriteLine("The number is incorrect."); // Error message or whatever
}
}
public static bool IsValidCode(string number)
{
int[] lookup = new int[] // Precalculated conversions of digits.
{
0, // 0*2 = 0
2, // 1*2 = 2
4, // 2*2 = 4
6, // 3*2 = 6
8, // 4*2 = 8
1, // 5*2 = 10 = 1+0 = 1
3, // 6*2 = 12 = 1+2 = 3
5, // 7*2 = 14 = 1+4 = 5
7, // 8*2 = 16 = 1+6 = 7
9 // 9*2 = 18 = 1+8 = 9
};
bool doLookup = true;
int sum = 0;
foreach (char c in number)
{
int digit = c - '0';
if (doLookup)
sum += lookup[digit];
else
sum += digit;
doLookup = !doLookup;
}
return ((sum%10) == 0 );
}
}
CzarOfKatmandu
Computertech156
Thank you Matthew for your quick reply.
The thing is, I'm just beginning to learn C# (and programming for that matter) and I don't really know how to implement your answer in my code...
I just want my program to ask the user for his birth date / birth number combination (10 digits), figure out if the number is correct or incorrect, and finally write the result on the screen. Something like this:
Console: "Your birth date and birth number (YYMMDDNNNN) "
User: "XXXXXXXXXX"
Console: "The number is correct." or "The number is incorrect."
That's about it...
In your reply, were you referring to my string number in the following line
Hope you can help me a little bit more, and thanks for your time.
Best regards,
Magnus Eklund
Derek Haskin
bool IsValidCode(string code)
{
int[] lookup = new int[] // Precalculated conversions of digits.
{
0, // 0*2 = 0
2, // 1*2 = 2
4, // 2*2 = 4
6, // 3*2 = 6
8, // 4*2 = 8
1, // 5*2 = 10 = 1+0 = 1
3, // 6*2 = 12 = 1+2 = 3
5, // 7*2 = 14 = 1+4 = 5
7, // 8*2 = 16 = 1+6 = 7
9 // 9*2 = 18 = 1+8 = 9
};
bool doLookup = true;
int sum = 0;
foreach (char c in code)
{
int digit = c - '0';
if (doLookup)
sum += lookup[digit];
else
sum += digit;
doLookup = !doLookup;
}
return ((sum%10) == 0 );
}
walker4bc
Sorry to bother you again, and thanks again for your help.
System;So this is what I've got so far...but it won't compile...can you see what's missing Or am I forgeting something
using
public
class TryNumber{
public static void Main()
{
string number;
Console.Write("Your birth date and birth number (YYMMDDNNNN) ");
number = Console.ReadLine();
if (IsValidCode(number))
{
Console.WriteLine("The number is correct."); // Processing if valid.
}
else
{
Console.WriteLine("The number is incorrect."); // Error message or whatever
}
bool IsValidCode(string number)
{
int[] lookup = new int[] // Precalculated conversions of digits.
{
0, // 0*2 = 0
2, // 1*2 = 2
4, // 2*2 = 4
6, // 3*2 = 6
8, // 4*2 = 8
1, // 5*2 = 10 = 1+0 = 1
3, // 6*2 = 12 = 1+2 = 3
5, // 7*2 = 14 = 1+4 = 5
7, // 8*2 = 16 = 1+6 = 7
9 // 9*2 = 18 = 1+8 = 9
};
bool doLookup = true;
int sum = 0;
foreach (char c in number)
{
int digit = c - '0';
if (doLookup)
sum += lookup[digit];
else
sum += digit;
doLookup = !doLookup;
}
return ((sum%10) == 0 );
}
}
Best regards,
Magnus Eklund
Thomas Lau
thx , Greg