Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Search:
   Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML Facing Programming Problem? Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Write a program to calculate factorial of any given number
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.

 

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

8 comments so far (
post your own) View All Comments Latest 10 Comments:

so nice program

Posted by Shane on Wednesday, 08.6.08 @ 11:34am | #71278

What if we want to print the number. Like 3!. 3*2*1

Posted by Rizky Ridho on Tuesday, 04.15.08 @ 23:37pm | #56632

i dint the understand the basics at all about the usage of BufferedReader object

Posted by Shanks on Monday, 05.14.07 @ 16:37pm | #15854

Dear sir,

The updated example descreption works for 0 to 170 , above that it shows "infinity". Just add the right code so that it will work for a bigger number.

thanx

Posted by ALOK RANJAN UDGATA on Friday, 03.23.07 @ 12:54pm | #12549

Hi,
Dear learner!

Here is an updated example to solve your problems. First example only evaluates the factorial of numbers between 0 and 13 and another is evaluated the factorial of all numbers that lies in double data type in java.

Factorial Large Number

vinod kumar

Posted by vinod kumar on Monday, 03.5.07 @ 17:50pm | #10738

Factorial 41 is an enormous number. Fact should be defined as "long", not "int" to cope.

Posted by Kim Tran on Friday, 02.2.07 @ 11:06am | #5379

this doesnt work for numbers >= 13!

Posted by poornima d on Friday, 01.19.07 @ 21:16pm | #3850

this example does not works for values of 'a' greater than 41.

Posted by Prashant Tiwari on Friday, 01.5.07 @ 17:57pm | #2715

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.