Computing.Net > Forums > Programming > 'Factorial' Numbers in VB6

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

'Factorial' Numbers in VB6

Reply to Message Icon

Name: bertyzz
Date: February 13, 2003 at 15:11:35 Pacific
OS: XP
CPU/Ram: 1.1gig 256DDR
Comment:

Hi Guys,
Im having a spot of bother trying to write a program that will produce a 'Factorial' result of a given number, eg:

The 'Factorial' number of 10 is 3628800

This sum is worked out by performing the following calculation:

10!=10x9x8x7x6x5x4x3x2x1 =362880

I have tried using a loop & a counter to generate the decrements of the number 10, but I cant get it to work. I either get a result of 0 or a huge result, both of which are incorrect.

Can anyone shed a bit of light on this for me?

Thanks in advance.

Bertyzz



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff Graver
Date: February 13, 2003 at 16:28:43 Pacific
Reply:

Both of your strange results could be the result of uninitialized data. If, for example, you have a variable called "fact" and you simply start multiplying fact times a loop counter without initializing it, fact might have been 0 to start. In which case it'll be 0 at the end, too, since 0*anything = 0. If fact had some random bit of data, then going through your loop will generate a very large number, specifically 10! times the random data (10! is not a small number itself, so you can see how this number could get really, really big). Make sure the first thing you do is assign the value 1 to the variable you use to accumulate the multiplications.

Jeff


0

Response Number 2
Name: StinkyToes
Date: February 13, 2003 at 16:37:07 Pacific
Reply:

Have to hate those spots of bother :).

You should be able to modify the following code to suit your needs. The code is assuming two text boxes and a command button. Put you number in the first text box, and click the button to get the factorial in the second box.

Private Sub Command1_Click()

Dim x As Integer
Dim total As Long

total = CLng(Text1.Text)

For x = Int(Text1.Text) - 1 To 2 Step -1

total = total * x

Next

Text2.Text = total

End Sub


0

Response Number 3
Name: bertyzz
Date: February 14, 2003 at 04:50:10 Pacific
Reply:

Thanks guys, I am now officially out of my spot of bother!!!

StinkyToes: Thanks for showing me that code, by looking at the code you wrote I was able to see where I went wrong!

Thanks

Bertyzz


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


ansi 98 c++ standard chan... Get Application Directory...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: 'Factorial' Numbers in VB6

Syntax in VB6 www.computing.net/answers/programming/syntax-in-vb6/15565.html

One line of a .txt file in VB6 www.computing.net/answers/programming/one-line-of-a-txt-file-in-vb6/8248.html

Randomizing Numbers in VB6 www.computing.net/answers/programming/randomizing-numbers-in-vb6/673.html