Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Write a program to calculate factorial of any given number

 
 

Share on Google+Share on Google+

Write a program to calculate factorial of any given number

Advertisement
This tutorial will teach you the methods for writing program to calculate factorial of any given number.

Factorial Examples - Java Factorial Example to calculate factorial of any given number

     

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:

import java.io.*; 
class Factorial{
  public static void main(String[] args) {
  try{
  BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("enter the number");
  int a= Integer.parseInt(object.readLine());
  int fact= 1;
  System.out.println("Factorial of " +a+ ":");
  for (int i= 1; i<=a; i++){
  fact=fact*i;
  }
  System.out.println(fact);
  }
  catch (Exception e){}
  }
}

Download this example.

Updated Example description

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.

Here is the code of program:

import java.io.*; 

class Factorial1{
  public static void main(String[] args) {
  try{
  BufferedReader object = new BufferedReader(
 
new InputStreamReader(System.in));
  System.out.println("enter the number");
  int a= Integer.parseInt(object.readLine());
  double fact= 1;
  System.out.println("Factorial of " +a+ ":");
  for (int i= 1; i<=a; i++){
  fact=fact*i;
  }
  System.out.println(fact);
  }
  catch (Exception e){
  System.out.println("Array out of bounds exaception");
  }

  }
}

 

Download this example.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Related Tutorials

Discuss: Write a program to calculate factorial of any given number   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:31
Lavas
August 21, 2012
comment

Hi, im a begginner, so how do i run it?
maruti
September 12, 2012
java

java is best subject
selva
September 16, 2012
this is correct one

this is correct: fact=fact*a;
ranjeet kumar
October 11, 2012
java swing

sir, how to display 12 month calendar structure format in frame when i enter year,i only want its interface code plzs suggest me
confuzed
November 15, 2012
the dificulty of java

this program gave me cancer
pauline
November 16, 2012
hevlo everyone :)

i0 want to know more about this java programming
tahrir kilafah
May 2, 2013
computer since

how i can make database
Nithin joseph
August 16, 2013
program

factorial system definde program
Nithin joseph
August 16, 2013
Questions

factoriyal program all?
DMCA.com