# Factorial of the Given Number in Java Example

by Dinesh Thakur Category: Control Structures

Factorial Number is the product of natural numbers from one to that particular number.  Mathematically, n! = 1 * 2 * 3 * .... * (n-1) * n

Algorithm for Factorial of the Given Number:

step 1: Set fact= 1

step 3: Set c=b

step 4: Repeat through step-6 until (c > 0)

step 5:  fact=fact*c

step 6:  c=c-1

step 7: Print fact

step 8: Exit

Here is the Java Example for Factorial of the Given Number:

```import java.util.Scanner;public class FactorialNumber{         public static void main(String ars[])       {               int fact=1,b,c;            Scanner s=new Scanner(System.in);            System.out.print("Enter A Number for get Factorial : ");            b=s.nextInt();            c=b;            while(c>0)                  {                    fact=fact*c;                            c-- ;                   }            System.out.println();            System.out.println("Factorial of "+b+" is : "+fact);         }}
```

```/* Factorial Program in Java using BufferedReader */

import java.io.*;class FactorialBufferedReader{    public static void main(String args[] )    throws IOException    {        BufferedReader fact = new BufferedReader(new InputStreamReader (System.in));        String m;        int n,s=1,i;        System.out.print("Enter a Number : ");        m=fact.readLine();        n=Integer.parseInt(m);        for(i=1;i<=n;i++)        {            s=s*i;        }        System.out.println("Factorial is : "+s);    }}

```  Dinesh Thakur holds an B.C.A, 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.

Related Articles