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