Factorial Number In Java

We are back to this Java questions and programming question series. In this blog post, we are going to cover a very important programming question that is how will you find out the factorial number? In mathematics, we have seen factorial numbers. So now we will find out:

  • How will you find out the factorial number?
  • What are different ways to find out the factorial number?
  • What are the different conditions you will handle?
Post Type:Java Programs For Beginners
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

It’s a ever green interview question in the interview, if you are going for Java interviewer or if you are going for automation interview.

So what do you mean by factorial number, Factorial of a positive integer (number) is the sum of multiplication of all the integers smaller than that positive integer.

Factorial number of 3 means I just need to write like this 3 x 2 x 1 is equal to 6 right same thing if I ask you what is a factorial of 4 so factorial 4 is like this 4 x 3 x 2 x 1.

Note: Factorial of 1 is 1 and Factorial of 0 (zero) is 1.

Factorials of Numbers 1 to 10 Table

Here is the List Of factorials numbers from 1 to 10.

Factorial of a Number!         ExpansionValue
1!11
2!2 × 12
3!3 × 2 × 16
4!4 × 3 × 2 × 124
5!5 × 4 × 3 × 2 × 1120
6!6 × 5 × 4 × 3 × 2 × 1720
7!7 × 6 × 5 × 4 × 3 × 2 × 15,040
8!8 × 7 × 6 × 5 × 4 × 3 × 2 × 140,320
9!9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1362,880
10!10 × 9 ×8 × 7 × 6 × 5 ×4 × 3 × 2 × 13,628,800

What are different ways to find out the factorial number?

There are two ways to find factorial of a number. We can find the factorial with the help of recursive and with help of non recursive.

Factorial Number With out Using Recursive Method

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FactorialNumberEx1 
{
	// Find Factorial Using Recursive Method
	public static int factoral(int num)
	{
		int fact=1;
		if(num==0)
			return 1;
		for(int i=1;i<=num;i++)
		{
			fact=fact*i;
		}
		return fact;
	}
	public static void main(String[] args) 
	{
		// fact (3)=3x2x1
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter A Number To Find out Factorial: ");
		int number=sc.nextInt();
		System.out.println("The Factorial of "+number+ " is =  "+factoral(number));		
	}
}

Output:

Enter A Number To Find out Factorial: 5
The Factorial of 5 is =  120

Factorial Number Using Recursive Method

If you’re working with Java, you may come across situations where a method needs to call itself. In these cases, the method is known as a recursive method, and the process is called recursion.

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class FactorialNumberEx2 
{
	// Find Factorial Using Recursive Method
	public static int factoral(int num)
	{
		if (num==0)
			return 1;
		else
			return (num * factoral(num-1));		
	}
	public static void main(String[] args) 
	{
		// fact (3)=3x2x1
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter A Number To Find out Factorial: ");
		int number=sc.nextInt();
		System.out.println("The Factorial of "+number+ " is =  "+factoral(number));		
	}
}

Output:

Enter A Number To Find out Factorial: 8
The Factorial of 8 is =  40320

FAQS on Factorial Number

What do you mean by recursive method?

A recursive method is a way of solving a problem that uses itself as the input. This is often used when it becomes necessary to solve an equation or system of equations by iteration. The process begins with one set of variables (known as the base case), and then proceeds recursively until all the desired solutions have been found.

What is a factorial of 10?

The Factorial of 10 is calculated by multiplying 10x9x8x7x6x5x4x3x2x1=3628800

What is the symbol of factorial?

The factorial function is a mathematical formula represented by an exclamation mark.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment