Hi friend, hope you are doing good. In today’s tutorial, I am going to share a very useful **java program to calculate the factorial number** of any given number. If you are looking for how to write a program to calculate factorial number using Java programming language.

Copy the below program and execute it with the help of Java compiler to **calculate factorial numbers**.

Java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; class Factorial { public static void main(String args[]) { int n, c, fact = 1; System.out.println("Enter an integer to calculate the factorial:"); Scanner in = new Scanner(System.in); n = in.nextInt(); if ( n < 0 ) System.out.println("The entered number shouldn't be negative."); else { for ( c = 1 ; c <= n ; c++ ) fact = fact*c; System.out.println("Factorial of "+n+" is: "+fact); } } } |

Enter an integer to calculate the factorial:

-10

Entered number shouldn’t be negative.

#second try

Enter an integer to calculate the factorial:

5

Factorial of 5 is: 120

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

- Java program to calculate power of a number
- How to remove duplicate characters from String in Java?
- Fibonacci Series in Java Without Recursion
- Iterate value through Java HashMap
- Java program to remove punctuation from a given string
- Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
- Java Program to Implement Gauss Seidel Method
- Java Program to Implement Gauss Jordan Elimination
- A Simple Merge based O(n) solution to find median of two sorted arrays
- Java program to count islands in boolean 2D matrix

Article Tags: factorial program in java, factorial program in java using recursion, factorial program in java with output, Factorial Program using loop in java, Java programs, Java programs with examples, Java programs with output, write a java program to check factorial number, write a java program to find factorial number