Factorial using Iterative Method:
import java.io.*;
public class Factorial
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Number:: ");
int n = Integer.parseInt(br.readLine());
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
System.out.println("\nFactorial of " + n + " is:: " + res);
}
}
OUTPUT::
Enter Number:: 10
Factorial of 10 is:: 3628800
Factorial using Recursive Method:
import java.io.*;
public class FactorialUsingRecusion
{
static int fact(int n)
{
int res;
if (n == 1)
return 1;
else
{
res = fact(n - 1) * n;
return res;
}
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Number:: ");
int n = Integer.parseInt(br.readLine());
int f = fact(n);
System.out.println("\nFactorial of " + n + " is:: " + f);
}
}
OUTPUT::
Enter Number:: 10
Factorial of 10 is:: 3628800