# Java - Factorial of a Number With out using Recursion

We can write a program to find the factorial of a given number using recursion and with out using recursion.

4! means = 1 * 2 * 3 * 4 = 24
5! means = 1 * 2 * 3 * 4 * 5 = 120  or (5 * 4!)
6! means = 1 * 2 * 3 * 4 * 5 * 6 = 720 or (6 * 5!)

Recursion meaning the function calls itself.

## Source Code

import java.io.*;

class Facotorial_NoRecursive {

public static long Fact(long n)

{

long result = 1;

for (int i = 1; i <= n; i++)

result = result * i;

return result;

}

public static void main(String[] args) {

String inpstring = "";

try

{

System.out.print("Enter a Number to Find Factorial:");

long n = Long.parseLong(inpstring);

long result = Fact(n);

System.out.println("Facorial of " + n + " is " + result);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

## Output

C:\Java\Samples>javac Factorial_Recursive.java

C:\Java\Samples>Java Factorial_Recursive

Enter a Number to Find Factorial: 5

Factorial of 5 is 120