Factorial program in Java using for loop code example
Example 1: Factorial program in java
public class FactorialRecursion
{
public static void main(String[] args)
{
int factorial = 1;
int number = 6;
factorial = factorialFunction(number);
System.out.println("Factorial of " + number + " is : " + factorial);
}
static int factorialFunction(int num)
{
if(num == 0)
{
return 1;
}
else
{
return(num * factorialFunction(num - 1));
}
}
}
Example 2: factorial program in java
public class FactorialDemo
{
public static void main(String[] args)
{
int number = 6, factorial = 1;
for(int a = 1; a <= number; a++)
{
factorial = factorial * a;
}
System.out.println("Factorial of " + number + " is : " + factorial);
}
}