by Dinesh Thakur

This Java Factorial Example shows how to finds factorial of a given number using Java. Entered number is checked first if its negative then an error message is printed.


Here is the Java Example for the program Factorial : 

import java.util.Scanner;

public class Factorial
{
         public static void main(String[] args)
          {
        try
               {
            int number ;
           System.out.println("Enter an integer to Finds it's factorial");
           Scanner scan = new Scanner(System.in);
           number = scan.nextInt();
           if ( number < 0 )
           System.out.print("Number should be non-negative.");
           else
             {
                int fact = number;
                for(int i =(number - 1); i > 1; i--)
                   {
                      fact = fact * i;
                   }
                System.out.println("Factorial of a number is " + fact);
             }
         }
               catch (Exception e){}
     }
}
Output of program:
Factorial Examples - Java Factorial Example to calculate factorial of any given number