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: How will you find 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 the 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 an evergreen interview question whether you are going for a Java or an automation interview.

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

A factorial number of 3 means I just need to write like this: 3 x 2 x 1 is equal to 6. Right? If I ask you what a factorial of 4 is, factorial 4 is like this: 4 x 3 x 2 x 1.

Note: The factorial of 1 is 1, and the Factorial of 0 (zero) is 1.

Factorials of Numbers 1 to 10 Table

Here is the List Of factorial 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 the different ways to find out the factorial number?

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

Factorial Number Without 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

Avatar for Softwaretestingo Editorial Board

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