factorial using for loop

Page 1 of 1

7 Replies - 77383 Views - Last Post: 03 September 2011 - 07:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=51520&amp;s=d89367f036dea9fa879f22f2265e0be5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 bliss

Reputation: 0
• 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???

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

• D.I.C Regular

Reputation: 14
• Posts: 343
• 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

#3 bliss

Reputation: 0
• 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

#4 harmonysharmonys

Reputation: 0
• 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

#5 harmonysharmonys

Reputation: 0
• 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

#6 maraqa

Reputation: 0
• 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
```

#7 harmonysharmonys

Reputation: 0
• 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.

#8 x_guru

Reputation: 0
• 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