This Java programming tutorial will teach you the methods for writing program to calculate factorial of any given number. First of all define a class "Factorial" under the Java I/O package. Java I/O package has a input stream and a output stream in which input stream is used for reading the stream and memory allocating and the output stream is used for writing bytes. As in this program we are going to insert certain instruction by creating buffer reader class, it is necessary to use 'try' and 'catch' block for catching and handling exceptions during execution of the program.
Here, we have to create a buffer for the string class that can be used to instantiate a changeable object for storing and processing a string of character. The strings length and content change as soon as any object is inserted, replaced or removed from the StringBuffer object.
Now create a buffer object that inherits properties from the string object class. Now create an InputStreamReader that reads bytes and decodes them into character by using certain 'charset'. Now use the ParseInt method for converting the parses the string argument to a decimal integer and define 'a' as an integer. Take an integer variable as fact=1 and insert the message in the System method.
Now applying for loop with conditions as integer i=1(intializer), i<=a and i++ as increment operator. So output result will be like fact=fact*i.
Here is the code of the program:
This is the updated example of factorial that will evaluates
the factorial of number in double which is lies in the range of double data type in java.
Updated Example description
Here is the code of program:
Posted on: June 4, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles