How to find the factorial of a number in java
Algorithm to find factorial of a number
 import java.io.*; and import java.util.Scanner; // package
 Create: class Factorial{
 Create the main function:
 public static void main(String args[])
 create integer variable:
 int n, c, fact = 1;
 Print the message to use to enter an integer to calculate it’s factorial
 Scanner in = newScanner(System.in);
 Read: n = in.nextInt();
 if( n < 0){
 System.out.println(“Number should be nonnegative.”);

else {

for ( c = 1 ; c <= n ; c++ )

fact = fact*c;

System.out.println(“Factorial of “+n+” is = “+fact);
Source Code:
import java.io.*;
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 it's factorial:");
Scanner in=new Scanner(System.in);
n =in.nextInt();
if ( n < 0 )
System.out.println("Number should be nonnegative.");
else
{
for(c= 1 ; c <=n ; c++)
fact =fact*c;
System.out.println("Factorial of "+n+" is ="+fact);
}
}
}
Expected Output:
FAQ:
Is there a factorial function in Java?
No there is not such predefine factorial function but if we need we can create as user define factorial function.
How do you calculate 100 factorial in Java?
In the below you can see the simple code to calculate the factorial of 100 in java.
int c, fact = 1;
int n= 100
if( n< 0 )
System.out.println(“Number should be nonnegative.”);
else
{
for (c = 1 ; c<= n ; c++ )
fact= fact*c;
System.out.println(“Factorial of “+n+” is =”+fact);
Recommended Post:
 Python Odd and Even  if the condition
 Python Greater or equal number
 Python PALINDROME NUMBER
 Python FIBONACCI SERIES
 Python Dictionary  Items method  Definition and usage
 Python Dictionary  Add data, POP, pop item, update method
 Python Dictionary  update() method
 Delete statement, Looping in the list In Python
 Odd and Even using Append in python
 Python  Simple Odd and Even number
Get Salesforce Answers
6 thoughts on “Write a Java Program that find the factorial of a number”