Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi Guys,
Can anyone help me out with my problem?
Here it goes......Is it possible to count alternative numbers that have been entered via a text box?
eg, enter the numbers:
123456789
Then count the total value of the even numbers (2,4,6,8) and display their total value, i.e, (20).Any help would be great guys.
Bertyzz

here what i would do:
Dim str As String
Dim i As Integer
Dim strAns As String
str = Text1
For i = 2 To Len(str) Step 2
strAns = strAns & Mid(str, i, 1) & " "
Next
Text2 = strAnsbut this will only work from 1-9 as input, no double digit numbers for this code.

have a counter loop, kinda like the one above...im to lazy to copy. but add this:
if int(counter /2) = counter / 2 then
'you know its an even number, and you should add that value to the variable that will be your total.
else
'dont add it
end if

Hi guys,
Thanks for your help! I understand what you are both saying but what I need to do is actually display the total value of the even numbers:eg; 123456789
The even numbers from the string above add up to 20.
Please help meeeeee.
Thanks guys
Bertyzz

I'm not sure how the numbers are being entered into the textbox, but if you had two textboxes with the first and last number this would work:
Private Sub Command1_Click()
Dim text As String
Dim FirstNum As Integer
Dim LastNum As Integer
Dim Total As Integer
Dim Counter As Integer
FirstNum = Val(Txt1.text)
LastNum = Val(Txt2.text)
'Intialize Total
Total = 0
For Counter = FirstNum To LastNum
If Counter Mod 2 = 0 Then
Total = Total + Counter
End If
Next Counter
lblDisp.Caption = Total
End SubIf there was one textbox I wouldn't know how to strip the numbers off though.

G'day,
Not sure about VB6 but does it support QBASIC commands?
If so, this is how you can do it:test$ = "123456789"
ln% = LEN(test$)FOR index% = 1 TO ln% STEP 1
IF VAL(MID$(test$, index%, 1)) MOD 2 = 0 THEN
sum% = sum% + VAL(MID$(test$, index%, 1))
END IF
NEXT
PRINT "The total is:", sum%This, as mentioned previously, only works for single digits (mod isn't too difficult ,though)
However, I think from this and the previous post, you can work out the algorithm and adapt it to VB6.
regards,
Elric

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |