Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   Help - Get the product of the numbers between 1 and n? (http://www.programmingforums.org/showthread.php?t=12183)

dwizzle13 Dec 14th, 2006 5:20 PM

Help - Get the product of the numbers between 1 and n?
 
How can I change this code to get the product of the numbers between 1 and a variable n. Here's the code, and I can just get the n's square.

:

    Function product(ByVal n As Double, ByRef i As Double)
        Dim a As Double
        For i = 1 To n
            a = n * i
        Next
        Return a
    End Function


Thanks.

Pizentios Dec 14th, 2006 6:18 PM

well, right now you are reseting the varible "a" to a new number everytime your loop runs.

you need to add the value to a, here's how i would do it.

:

Function product(ByVal n As Double, ByRef i As Double)
      Dim a As Double
      a = 0
      for i = 1 To n
              a = a + (i*n)
      Next
      Return a
End Function


not sure if that's what you are looking for. Also, this looks like homework....please don't let it be homework, mainly because posting homework questions is against the forum rules.

dwizzle13 Dec 14th, 2006 8:33 PM

thanks
 
Thanks, no, it's for myself. I'm trying to learn vb from an old book, but I didn't know what to do.

Pizentios Dec 15th, 2006 11:09 AM

ah ok, cool...for i minute there i thought i was doing your homework (we get a lot of people trying to get us to do home work questions).


All times are GMT -5. The time now is 1:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC