Wednesday, January 28, 2015

A Simple Factorial Program

      For this assignment, we needed to make a simple program to show what we have learned so far in our knowledge of Java. For this, my partner and I created a program that will calculate the factorials of a number. The factorial command is represented by a ! mark, and is a mathematical function that multiplies the factors of a number together.

For example Factorial two, or 2! would be 2*1, which equals 2. Factorial 4 (4!), is 4*3*2*1, which is 24. As the numbers get higher, it becomes quite the ordeal to multiply these out, and a factorial calculator becomes very useful.

The code is displayed below.


Because of the limitations of the standard int (integer) variable type in Java, the maximum our calculator can calculate without errors is factorial 13, as any number larger than that creates a value too large for the standard Java variable to handle.

To make this program work we used For loops to run the logic of the actual factorial maths. We learned about for loops many times through the different languages we have learned so far and they proved to be invaluable in making the code more efficient. At the end of the code, the result is returned to the user as all of the factorial values from 1 to 13.

No comments:

Post a Comment