Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 86,390 VB.NET Programmers. There are 1,396 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a VB.NET Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

factorial using for loop

 
Reply to this topicStart new topic

factorial using for loop

bliss
post 9 May, 2008 - 04:12 AM
Post #1


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 3



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

vb

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.gif

This post has been edited by PsychoCoder: 9 May, 2008 - 06:36 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


xtreampb
post 9 May, 2008 - 05:35 AM
Post #2


New D.I.C Head

*
Joined: 20 Jun, 2007
Posts: 28

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
CODE
i=i*i

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


when you are using "&" that is the same thing as
CODE
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: 9 May, 2008 - 05:38 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

bliss
post 11 May, 2008 - 07:23 PM
Post #3


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 3

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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

harmonysharmonys
post 12 May, 2008 - 07:20 AM
Post #4


New D.I.C Head

*
Joined: 12 May, 2008
Posts: 3

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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

harmonysharmonys
post 12 May, 2008 - 07:27 AM
Post #5


New D.I.C Head

*
Joined: 12 May, 2008
Posts: 3

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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

maraqa
post 12 May, 2008 - 10:41 AM
Post #6


New D.I.C Head

*
Joined: 28 Apr, 2008
Posts: 1

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

CODE
  For i = 1 To 5  
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

harmonysharmonys
post 12 May, 2008 - 11:18 AM
Post #7


New D.I.C Head

*
Joined: 12 May, 2008
Posts: 3

No. It should give you the same result, because of the commutative property.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 05:02AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month