954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding the sum of odd numbers. Please help

I have been trying to figure this out for quite some time... Some help would be appreciated :)

intnumber = txtnum.Text
intcount = 1
Do While lngsum <= intnumber
intcount = intcount + lngsum
lngsum = lngsum + 2
Loop
lblsum.Caption = "The sum of the odd numbers is: " & intcount

t3hfr3ak
Newbie Poster
22 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Correct me if I'm wrong. The user enters a number and the app is supposed to add each odd number between 0 and the input? Your program, on the other hand, adds each even number less than or equal to the input then adds 1 to the total sum. To correct this, one way is that intcount = 1 should be lngsum = 1

scudzilla
Posting Whiz in Training
277 posts since Mar 2007
Reputation Points: 27
Solved Threads: 17
 

:o I got it
Option Explicit
Dim intnumber As Integer
Dim lngsum As Long
Dim intcount As Integer

Private Sub cmdcalc_Click()
intcount = 0
intnumber = txtnum.Text
lngsum = 1
Do While lngsum <= intnumber
intcount = intcount + lngsum
lngsum = lngsum + 2
Loop
lblsum.Caption = "The sum of the odd numbers is: " & intcount
End Sub

Private Sub txtnum_Change()
lblsum.Caption = "The sum of the odd numbers is: "
End Sub

t3hfr3ak
Newbie Poster
22 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

This will work just as well

lblsum.Caption = "The sum of the odd numbers is: " & round((intnumber / 2) + .1) ^ 2

Cheers

D

davidcairns
Junior Poster
114 posts since Feb 2007
Reputation Points: 12
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You