Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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

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 Longtotal = CLng(Text1.Text)
For x = Int(Text1.Text) - 1 To 2 Step -1
total = total * x
Next
Text2.Text = total
End Sub

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

![]() |
ansi 98 c++ standard chan...
|
Get Application Directory...
|

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