Factorials are used to calculate combinations of things in statistics. The factorial of an integer is the product of that integer times all positive numbers less than itself. For example 5 factorial is:
5 * 4 * 3 * 2 * 1 or 120.
The symbol for factorials is the exclamation point, so 5 factorial is written like this:
By the way, we programmers call the exclamation point a bang. So we would say “5 bang” to describe this. Just another reason to become a programmer. Cool jargon.
In statistics, factorials are used to calculate permutations and combinations. For example, the odds of getting a particular poker hand can be calculated using factorials.
As a gambling craze has hit downtown Rome and the Romans are as innumerate as ever, you have decided to help them calculate factorials. Your program will look simple. The Roman will enter an integer, and you will display the factorial of that integer. One problem you need to consider is that factorials get very large very quickly. It doesn’t take a very large integer to have a very large factorial. You need to figure out how large an integer can be held in a VB integer variable and check that you don’t enter a number that’s too large. You will also need to know how to construct a loop. Three loop constructs exist that you can use (you only need one of these):
For – Next
Before you start your program you consult with a mathematician who informs you that the factorial of zero is 1, and that there is no such thing as factoring a negative number. You thank her and decide to put these checks in your program.
You will also need to use the integer data type instead of the decimal data type. Variables of type integer can only hold whole numbers – no fractions. They are declared like this:
Dim intFactorial as int
Also the integer.tryparse function will probably be useful. Integer.tryparse does what you would expect it to do.