Calculate Factorial of Number in Java

By | January 10, 2016

Calculate Factorial of Number in Java Programming

Here, we have listed How To Calculate Factorial of Number in Java Programming Language. It is important that we should know How A For Loop Works before getting further with the Java Program Code.

What is a Factorial of an Integer?

A Factorial for a Non-Negative Number or Integer is the Product of all the Positive Integers Less than or Equal to that Number.

Example

6! = 6 * 5 * 4 * 3 * 2 * 1 = 720

Also Read: Java Program To Find Palindrome Numbers

Method 1: Calculate Factorial of Number in Java using While Loop

import java.io.*;

class Factorial
{
    public static void main(String args[]) throws Exception
    {
        int fact = 1, num, count;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\nEnter an Integer:");
        num = Integer.parseInt(br.readLine());
        for(count = num;count > 0;count--)
        {
            fact = fact * count;
        }
        System.out.println("\nFactorial of " + num + ":\t" + fact);
    }
}

Output

Calculate Factorial of Number in Java Programming

To know more about Factorial of Number in Java Language, visit WikiPedia. In case you get any Compilation Errors with this Java Program To Calculate Factorial of a Number or if you have any doubt about it, mention it in the Comment Section.

Let's Discuss