Computing.Net > Forums > Programming > Counting alternating numbers in VB6

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Counting alternating numbers in VB6

Reply to Message Icon

Name: bertyzz
Date: February 23, 2003 at 11:54:50 Pacific
OS: XP
CPU/Ram: 1.3gig
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: BelAnWel
Date: February 23, 2003 at 13:21:25 Pacific
Reply:

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 = strAns

but this will only work from 1-9 as input, no double digit numbers for this code.


0

Response Number 2
Name: eaw8806
Date: February 23, 2003 at 14:12:50 Pacific
Reply:

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


0

Response Number 3
Name: bertyzz
Date: February 23, 2003 at 14:43:36 Pacific
Reply:

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


0

Response Number 4
Name: Burgermeister
Date: February 23, 2003 at 18:01:52 Pacific
Reply:

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 Sub

If there was one textbox I wouldn't know how to strip the numbers off though.


0

Response Number 5
Name: elric
Date: February 23, 2003 at 23:03:09 Pacific
Reply:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Counting alternating numbers in VB6

'Factorial' Numbers in VB6 www.computing.net/answers/programming/factorial-numbers-in-vb6/5520.html

Syntax in VB6 www.computing.net/answers/programming/syntax-in-vb6/15565.html

count # of characters in file + EOF www.computing.net/answers/programming/count-of-characters-in-file-eof/4457.html