by Dinesh Thakur Category: Data Type

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)
            int number ;
           System.out.println("Enter an integer to Finds it's factorial");
           Scanner scan = new Scanner(;
           number = scan.nextInt();
           if ( number < 0 )
           System.out.print("Number should be non-negative.");
                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

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content