Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- import java.io.*;
- class factorial
- {
- long Factorial(int a)
- {
- if(a == 1)
- return 1;
- else
- return(a*Factorial(a-1));
- }
- }
- class CalculateFactorial
- {
- public static void main(String args[])
- {
- DataInputStream in = new DataInputStream(System.in);
- int number;
- long answer;
- try
- {
- factorial f1 = new factorial();
- System.out.print("Enter Number: ");
- number = Integer.parseInt(in.readLine());
- if(number > 0){
- answer = f1.Factorial(number);
- System.out.println("Factorial is : " + answer);
- }
- else{
- System.out.println("Invalid Number");
- }
- }
- catch(Exception e)
- {
- System.out.println("Exception" + e);
- System.exit(1);
- }
- }
- }

Post new comment