|
-
Nov 3rd, 1999, 06:23 AM
#1
Thread Starter
Lively Member
You are to write a function that will calculate a factorial of a number from 1 to 150. The user should enter a number followed by a ! the symbol for a factorial in a text box on a form. When the user clicks a command button you should error check for a blank box, or other inappropriate entries. Once satisfied that the data is valid, the code should call a function called MYFACTORIAL. Once the factorial has been calculated the result should be placed in a label called lblfactorial.
To calculate a factorial you multiply each number from 1 up to and including the number which the factorial is being taken of. For example:
6! Would be calculated thusly: 1 x 2 x 3 x 4 x 5 x 6
6! = 720
11! Would be calculated thusly: 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11
11! = 39,916,800
Below is an example of a function that takes to numbers and returns a number that has been truncated to the number of decimals that was specified:
Public Function Truncate_Decimals(numberin As Single, places As Integer)
'cuts off decimals after a specified number of places
Truncate_Decimals = Int(numberin * 10 ^ places) / 10 ^ places
End Function
If the user enters 3.123456789 in a text box a program could use this function to trim it to any number of decimal places to the right of the decimal point. The result of using this function would be 3.1234 in the variable Trimmednumber.
Trimmednumber = Truncate_decimals(val(txtnumberin.text),4) ‘ trims number in text box to four digits to theright of the decimal place
-
Nov 3rd, 1999, 06:28 AM
#2
Hyperactive Member
Ok, so what exactly is your question?
------------------
Thanks,
Ryan
[email protected]
ICQ# 47799046
-
Nov 3rd, 1999, 07:16 AM
#3
Hyperactive Member
and why should we do your homework? if you have a specific question...
-
Nov 3rd, 1999, 07:23 AM
#4
Thread Starter
Lively Member
Just someone tell me. How could I possibly make the number 6, be calculated as:
1*2*3*4*5*6.
I don't quite understand how I would go about this. Otherwise I understand the project completely.
------------------
"I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"
-
Nov 3rd, 1999, 07:35 AM
#5
Hyperactive Member
ok, here's what you would do. text1 is where the number is inserted that you want to find the factorial of. text2 is where the answer will be displayed.
Dim x
factorial = text1.text
For i = 1 to factorial
x = x * i
Next i
Text2.text = x
There you go, it's that simple. Just put this code where you want the actual calculation to be carried out, probably a button that's marked "calculate" or something like that. Have fun on your homework.
------------------
Thanks,
Ryan
[email protected]
ICQ# 47799046
-
Nov 3rd, 1999, 07:52 AM
#6
Thread Starter
Lively Member
Surely, it's not that simple because x is always equal to zero. Because it's value never increases. So everytime it calculates, here is what the program will see, numberwise.
Dim x(which at this point is given the default of zero)
factorial = a number, let's say 3
For i = 1 to 3
x(which equals zero) = x(zero) * 1
'This results in x = 0
Next
then it goes again,
x(which still equals zero) = x(zero) * 2
'So now, even though the number you were multiplying x by increases, x still equals zero.
Text2.text = x 'Which at this point, still contains no value.
I might be wrong, is there anyone who can show me otherwise and make me look like a total fool? Please say yes.
------------------
"I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"
-
Nov 3rd, 1999, 07:56 AM
#7
Hyperactive Member
Ok, well then simply put this statement after Dim x:
x=1
That will solve your problem
------------------
Ryan
[email protected]
ICQ# 47799046
-
Nov 3rd, 1999, 09:19 AM
#8
Thread Starter
Lively Member
Did that, now I have another problem.
How do I check what the last character in a string is?
So far I've got:
dim length as integer
dim original as string
dim dog as string
original = "Bob!"
length = len(original)
dog = mid(original, length,1)
if dog like "!", wait...I've got it. Never mind.
------------------
"I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|