java program for factorial

java program for factorial


Factorial in java

A factorial of a a positive number is the product of 0 to n. Factorial of 0(zero) is 1. The factorial number is denoted by ! (Exclamation sign). Like n!
             n! = n*(n-1) * (n-2) * (n-3) * (n-4) . . . . . *1

Algorithm of Factorial number:
Step 1: Input the number in n
Step 2: Initialize a variable fact=1
Step 3: start the loop with condition i (initial value 1)
Step 4: Loop will iterate till n=6 number of times.
Step 5: Print the final value  720
Step 6: end

Factorial program using For Loop


public class FactorialWithForLoop

{
    public static void main(String args[]){ 
        int i,fact;   
        fact=1;       // defining fact=1 since least value is 1
        int num=6;    // given input to calculate factorial
        for(i=1;i<=num;i++)
        {   
            fact=fact*i; // Factorial Calculation 
        }   
        System.out.println("Factorial of "+num+" = "+fact);   
    } 
}
In the above program, the ‘num’ variable is taken as initial value 1 for factorial calculation. Then, we’ve used for loop to loop through all the numbers between 1 and the input numb(6), where the product of each num is stored in the variable ‘fact’. The loop will iterate 6 times and the result will be 720



Factorial program using While Loop



public class FactorialWithWhileLoop
{
    public static void main(String args[]){ 
        int i,fact;   
        fact = 1;     // defining fact=1 since least value is 1
        i = 1;        // i initialze as 1
        int num=6;    // given input to calculate factorial
        while(i<=num)
        {   
            fact=fact*i; // Factorial Calculation 
            i++;
        }   
        System.out.println("Factorial of "+num+" = "+fact);   
    } 

}
In the above program, the fact initializes to 1, i is 1 and num as 6.  The variable i is incremented inside the body of the loop. It will calculate the factorial and give result as 720

Factorial program in java using recursion method


A Recursion is a function which calls by itself. This enables the function to repeat itself several times, outputting the result and the end of each iteration. The following factorial program is recursive.
public class FactorialUsingRecursion
{
    int factorial(int n)
    {   
        if (n == 0)   
            return 1;   
        else   
            return(n * factorial(n-1));   
    }   

    public static void main(String args[])
    { 
        int i,fact, num; 
        num=6;  //It is the number to calculate factorial   
        FactorialUsingRecursion ob = new FactorialUsingRecursion();
        fact = ob.factorial(num);  
        System.out.println("The factorial of "+num+" is: "+fact);   
    } 
} 
In the above programming code, the factorial() method is recursive which calls itself until the condition is satisfied.

Factorial program in java using ternary operator


public class FactorialUsingTarnaryOperator
{
    int num;
    int factorial(int n)
    {
        // factorial using tarnary operator
        num = n;
        return (num == 1 || num == 0) ? 1 : num * factorial(num - 1);

    }

    public static void main(String args[])
    { 
        int i,fact, num; 
        num=6;   
        FactorialUsingTarnaryOperator ob = new FactorialUsingTarnaryOperator();
        fact = ob.factorial(num);  
        System.out.println("The factorial of "+num+" is: "+fact);   
    } 
}
In the above factorial () the method we have used ternary operator to get factorial.

   The following program will calculate the big number factorial. So for large number factorial, we need to use BigInteger class of java.math package.  The BigInteger class represents a large number. In this class, an overflow will not occur. Here fact and I variables use as BigInteger object and initialized with 1.
import java.util.Scanner;
import java.math.BigInteger;

public class FactorialUsingLargeNumber
{

    int n, j;
    void factorial(int x)
    {
        n=x;
        BigInteger i = new BigInteger("1");
        BigInteger fact = new BigInteger("1");

        for (j = 1; j <= n; j++)
        {
            fact = fact.multiply(i);
            i = i.add(BigInteger.ONE);
        }
        System.out.println(n + "! = " + fact);
    }

    public static void main(String args[])
    {
        int n;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number:");
        n = input.nextInt();
        FactorialUsingLargeNumber ob = new FactorialUsingLargeNumber();
        ob.factorial(n);
    }
}
Output:
Enter a number:
25
25! = 15511210043330985984000000


More Program

Java Program to Convert a Decimal Number to Binary Number



SHARE THIS
Previous Post
Next Post