Adding the sum of odd numbers. Please help

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 22
Reputation: t3hfr3ak is an unknown quantity at this point 
Solved Threads: 0
t3hfr3ak t3hfr3ak is offline Offline
Newbie Poster

Adding the sum of odd numbers. Please help

 
0
  #1
Apr 19th, 2007
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
Last edited by t3hfr3ak; Apr 19th, 2007 at 12:02 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 181
Reputation: scudzilla is an unknown quantity at this point 
Solved Threads: 3
scudzilla's Avatar
scudzilla scudzilla is offline Offline
Junior Poster

Re: Adding the sum of odd numbers. Please help

 
0
  #2
Apr 19th, 2007
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
There are 10 types of people: those who understand Binary and those who don't!
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 22
Reputation: t3hfr3ak is an unknown quantity at this point 
Solved Threads: 0
t3hfr3ak t3hfr3ak is offline Offline
Newbie Poster

Re: Adding the sum of odd numbers. Please help

 
0
  #3
Apr 19th, 2007
: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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 114
Reputation: davidcairns is an unknown quantity at this point 
Solved Threads: 8
davidcairns davidcairns is offline Offline
Junior Poster

Re: Adding the sum of odd numbers. Please help

 
0
  #4
Apr 19th, 2007
This will work just as well

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

Cheers

D
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC