PDA

Click to See Complete Forum and Search --> : Just tell me, what is my teacher talking about?


Apollo
Nov 3rd, 1999, 05:23 AM
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

Gimpster
Nov 3rd, 1999, 05:28 AM
Ok, so what exactly is your question?

------------------
Thanks,
Ryan
corneslen@hotmail.com
ICQ# 47799046

Bob Baddeley
Nov 3rd, 1999, 06:16 AM
and why should we do your homework? if you have a specific question...

Apollo
Nov 3rd, 1999, 06:23 AM
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!"

Gimpster
Nov 3rd, 1999, 06:35 AM
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
corneslen@hotmail.com
ICQ# 47799046

Apollo
Nov 3rd, 1999, 06:52 AM
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!"

Gimpster
Nov 3rd, 1999, 06:56 AM
Ok, well then simply put this statement after Dim x:

x=1

That will solve your problem

------------------
Ryan
corneslen@hotmail.com
ICQ# 47799046

Apollo
Nov 3rd, 1999, 08:19 AM
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!"