factorial program in java

Hello Friends,

This Java programming tutorial will teach you the methods for writing program to calculate factorial Number. First of all define a class “FactorialProg” 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.

In this Programme, we have to create a buffering 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.

Then, We 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 ‘num‘ as an integer. Take an integer variable as factorial=1 and insert the message in the System method.

Now applying for loop with conditions as integer i=1(intializer),  i<=num and i++ as increment operator. So output result will be like factorial=factorial*i.

Here is the Code :::

import java.io.*;
class FactorialProg{
public static void main(String[] args) {
try{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Please Enter The Number¬† :: “);
int num= Integer.parseInt(object.readLine());
int factorial= 1;
System.out.println(“Factorial of ” +num+ “:”);
for (int i= 1; i<=num; i++){
factorial=factorial*i;
}
System.out.println(factorial);
}
catch (Exception e){}
}
}

Enjoy Code…