7 Replies - 34659 Views - Last Post: 03 September 2011 - 07:25 AM Rate Topic: -----

#1 bliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 08

factorial using for loop

Posted 09 May 2008 - 04:12 AM

i tried do a factorial calculation using for loop from 1 to 5 as the code below ;

 Dim i As Integer


        For i = 1 To 5
            i = i * i 
            result.Text &= "hello world <br />" & i  
       next i



but the result i get is

hello world
1hello world
4hello world
25

what wrong with my loop???

EDIT: Code tags added, please use them => :code:

This post has been edited by PsychoCoder: 09 May 2008 - 06:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: factorial using for loop

#2 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: factorial using for loop

Posted 09 May 2008 - 05:35 AM

ok next time you post pleese put all your code in code tags i cant figure out how to show the tags without them actally exicuting but if you look in the text window before you start typing your response it will tell you how.

now i is decleared as in integer
so i is going to be mutiplyed by itself 5 times as decleared in
 i=i*i

but in the loop declearation it is also adding one to i
for i=1 to 5


when you are using "&" that is the same thing as
 string.concate()
so no matter if it is an integer or not it will intreperate whaterver is after the "&" as a string or character. hope this helps

This post has been edited by xtreampb: 09 May 2008 - 05:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 bliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 08

Re: factorial using for loop

Posted 11 May 2008 - 07:23 PM

hi xtreampb,

from ur reply mean that i need to declare another variable?

bcoz what i want is make it the "i" as an interger, then when "i" is time by itself then get the result, use the result to do the next "i"

eg:

1(i) *1(i -loop) = 1(i -result),

1(i-result)*2(i-loop) = 2(i-result)

2(i-result)*3(i-loop) = 6(i-result)

and so on....

the code i wrote above din't bring what i think the loop is?

This post has been edited by bliss: 11 May 2008 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 harmonysharmonys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-May 08

Re: factorial using for loop

Posted 12 May 2008 - 07:20 AM

Personally, I wouldn't use a for loop. I would use a While-Wend structure like this:

Private Function GetFact(ByRef num As Integer)

Dim fact As Integer = 1 'Assign this value seperately, if you are using VB6.
Dim i As Integer = 1 'Assign this value seperately, if you are using VB6.

if num > 1 then

While i <= num
fact = fact * i
i++
End While

End if

return fact

End Function

Additionally, Xtreampb failed to explain why you loop isn't working. It's because you are multiplying the counter variable at the same time you are trying to use it to keep track of your loop. This won't work. I would do it like this:

Private Function GetFactorial(ByRef num As Integer)

Dim fact As Integer = 1 'Assign this value seperately, if you are using VB6.
Dim i As Integer

if num > 1 then

For i = 1 To num

fact = fact * i

Next i

End if

Return fact

End Function


If num is 1 or 0, the factorial is 1. That's why you just return fact with its original assigned value, or 1, if num is not greater than 1.

num = whatever you want to figure the factorial of which is passed in to the function. The answer for 5 is 120 or 1*2*3*4*5 = 120.

Trust me, this is the correct way to do it.

This post has been edited by harmonysharmonys: 12 May 2008 - 09:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 harmonysharmonys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-May 08

Re: factorial using for loop

Posted 12 May 2008 - 07:27 AM

Just a note,

Xtreampb's suggestions won't work. I don't think he knows what factorial means.

Here's the issue: If you use the counter to calculate the factorial, and you multiply it in order to do this, how will it keep count?

Your counter will go 1, 2, 4, 16, etc. You need the counter variable to go 1, 2, 3, 4, 5.

Jon

This post has been edited by harmonysharmonys: 12 May 2008 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 maraqa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-April 08

Re: factorial using for loop

Posted 12 May 2008 - 10:41 AM

does it make any difference if u added step -1 so it wud look like this:

  For i = 1 To 5  

Was This Post Helpful? 0
  • +
  • -

#7 harmonysharmonys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-May 08

Re: factorial using for loop

Posted 12 May 2008 - 11:18 AM

No. It should give you the same result, because of the commutative property.
Was This Post Helpful? 0
  • +
  • -

#8 x_guru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-September 11

Re: factorial using for loop

Posted 03 September 2011 - 07:25 AM

I dont know why everyone is overcomplicating things here, this code works and is short and sweet:
    Private Function Factorial(ByVal inputval As Integer) As Integer
        Dim f As Integer
        Factorial = 1
        For f = 2 To inputval
            Factorial = Factorial * f
        Next
    End Function


This post has been edited by x_guru: 03 September 2011 - 07:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1