Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have to take a string of 5 numbers and write a function that will the second number and fourth number and multiply the numbers by 2. Then I have to take all the numbers add them together and take the last digit in the addition and check that it matches the last digit in the string.

 

Example

(12346) (2*2=4), (4*2=8, (1 + 4 + 3 + 8 = 16), The 6 in 16 validates with the 6 in (12346)

 

I hope that someone understands what I am trying to do. It is giving me a real fit. I have been trying to do this for two days with no luck.

Thanks for any help that you can give.

John

Posted (edited)

Are the numbers always 1 digit?

 

If so, you could go through and get each character pretty easily, otherwise you'd need something to seperate them to get each number.

if they're all 1 digit: then this SHOULD work, but i don't know

private function thing(text as string) as boolean
'Get an array of six numbers
       Dim number(5) As Integer
       'and a final number
       Dim finalnumber As Integer
       'Plug in their values according to the character in the string
       For x As Integer = 0 To 5
           number(5) = Integer.Parse(Convert.ToString(text.Chars(x)))
       Next
       'Do the math
       'remember, number 1 = the second number cause it starts at 0
       number(1) = number(1) * 2
       number(3) = number(3) * 2
       For x As Integer = 0 To 5
           'add them all up
           finalnumber += number(x)
       Next
       'now we get the last digit from finalnumber(there's gotta be an easier way
       Do While finalnumber > 9
           finalnumber = finalnumber - 10
       Loop
       If finalnumber = number(5) Then Return True Else Return False
end function

Edited by thenerd
Posted

I know the example that I gave was not that good. Say that it's a 5 digit number and I need to validate the 5th digit, that is when multiple the 2nd and fourth digit by 2 and add the results to the 1st and 3rd digit you take the total of the answer and if the last digit in the answer matches the 5th digit, it validates true, else false.

 

I hope this clears it up. I know there is an easier answer, I just can't figure it out. Maybe I am thinking to hard on it.

 

Thanks

John

  • Leaders
Posted

So... you want to multiply the second & fourth digits by two, and add them with eachother and the first and third digit, and then verify that the fifth digit is equal to the last digit in the sum of other digits... is that what you want? If so then here is an example...

 


   Function ValidateString(ByVal S As String) As Boolean
       Dim Values(4) As Integer
       Dim Chars As Char() = S.ToCharArray
       'You could just use the .substring function, but when I deal with characters
       '  i prefer to deal with Chars

       'You might want to test that s.Length is valid
       For i As Integer = 0 To 4   'Extract Values
           Values(i) = AscW(Chars(i)) - &H30 'Yes... you could also use Integer.Parse
           'My apologies to those that I might have offended by using functions from Microsoft.VisualBasic
           '  but VB doesn't treat chars as numbers the way C languages do
       Next

       Dim Sum As Integer = (Values(0) + Values(1) * 2 + Values(2) + Values(3) * 2)
       Return Sum Mod 10 = Values(4)
   End Function

 

That last line might confuse you but what it does is (Sum Mod 10) equals the last digit in your sum. This is compared to your last digit (Values(4)), your checksum of sorts. The function returns whether they are equal or not, and hence whether the string of digits is valid or not.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...