factorial program in java

Factorial Program in Java

Factorial related programs are very common in the programming world. These types of programs are very important and useful. Because it helps to improve mindset and programming skills. Before writing the Factorial Program in Java, we need to understand the meaning of Factorial.

What is Factorial?

The factorial of a integer n, denoted by n!, is the product of all positive integers less than or equal to n.
Factorial program in Java does not exist for negative numbers and factorial of 0 is 1. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence.

Basically, we find a factorial of a particular number. We can write a factorial of numbers like n!. “n” can be any positive number. For Better understanding please look below examples.
Example: n! = n*(n-1)*…….*1 .
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
In the above example, Factorial of 5 is 120. But in this tutorial, we will calculate factorial using Java Programming. But factorial of 0 (Zero) is always 1(One).

Factorial Program in Java

In Java Programming, we can write a program in the following ways.
1.) Factorial of a Number using For Loop.
2.) Factorial Program using While Loop.
3.) Factorial Program using Do-While Loop.
4.) Find Factorial of a number using recursion in java.
5.) Find Factorial of a number entered by the user in java.

Factorial Program in Java using For Loop

Factorial programs can be done in many ways. Because Looing is the main key for calculating the factorial of any number. In the below example, factorial will be calculated using for loop in java.

Now First we Go Through the Algorithm

Algorithm for Factorial program using For Loop

  1. Start
  2. Create an instance of the Scanner Class.
  3. Declare a variable.
  4. Ask the user to initialize the variable.
  5. Declare a loop variable and another variable to store the factorial of the number.
  6. Initialize both the variables to 1.
  7. Use a while loop to calculate the factorial.
  8. Run the loop till the loop variable is less than or equal to the number.
  9. Update the factorial in each iteration.
  10. Increment the loop variable in each iteration.
  11. Print the factorial of the number.
  12. Stop.
public class FactorialPractice {
    int fact_value = 1;
    int findFact = 5; // Find factorial of this number

    void findFactorial() {
        for (int i = 1; i <= findFact; i++) {
            fact_value = fact_value * i;
        }
        System.out.println("Factorial of " + findFact + " using For Loop is: " + fact_value);
    }

    public static void main(String[] args) {
        FactorialPractice factObj = new FactorialPractice();
        factObj.findFactorial();
    }
}


Output :

Factorial of a using For Loop is: 120

Factorial Program in Java using While Loop

Algorithm for Factorial program using While Loop

  1. Start
  2. Create an instance of the Scanner Class.
  3. Declare a variable.
  4. Ask the user to initialize the variable.
  5. Declare a loop variable and another variable to store the factorial of the number.
  6. Initialize both the variables to 1.
  7. Use a while loop to calculate the factorial.
  8. Run the loop till the loop variable is less than or equal to the number.
  9. Update the factorial in each iteration.
  10. Increment the loop variable in each iteration.
  11. Print the factorial of the number.
  12. Stop.

In this example, we will use while loop to find factorial. But the result will be the same.

public class FactorialPractice {
int fact_value=1;
int findFact=5; // Find factorial of this number
int i=1;
void findFactorial(){
       while(i<=findFact){
            fact_value=fact_value*i;
            i++;
       }
System.out.println("Factorial of a using For Loop is: "+fact_value);
}

// Main Class
public static void main(String[] args){
	FactorialPractice factObj=new FactorialPractice();
	factObj.findFactorial();
	}
}


Output :

Factorial of a using For Loop is: 120 

Factorial Program in Java using Do While Loop

Factorial programs can be done in many ways. Because Looing is the main key for calculating the factorial of any number. In the below example, factorial will be calculate using do-while Loop.

public class FactorialPractice {
int fact_value=1;
int findFact=5; // Find factorial of this number
int i=1;
void findFactorial(){
       do{
          fact_value=fact_value*i;
          i++;
       }
       while(i<=findFact);          
System.out.println("Factorial of a using For Loop is: "+fact_value);
}

public static void main(String[] args){
	FactorialPractice factObj=new FactorialPractice();
	factObj.findFactorial();
	}
}


Output :

 Factorial of a using For Loop is: 120 

Factorial Program in Java using Recursion

Recursion is a very interesting concept in Java. It is processed, when the function calls itself again and again. Factorial can also be found using recursion. Because of recursion, code can be efficient and easy.

Algorithm for Factorial program using Recursion

  1. Start
  2. Declare a variable to store a number.
  3. Ask the user to initialize the number.
  4. Check whether it is possible to calculate the factorial or not.
  5. If the number is greater than and equal to 0, then call a recursive function to calculate the factorial of the entered number.
  6. If the number is lesser than 0, print the message that it is not possible to calculate the factorial.
  7. If the entered number is 0 or 1, then return 1.
  8. If the entered number is other than 0 or 1, then calculate the factorial by recursively calling the same method.
  9. Return the result.
  10. Print the factorial of the entered number.
  11. Stop
import java.util.Scanner;
public class FactorialProgram {
    // Recursive Function to Find the Factorial of a Number
    public static int findFactorial(int num) {
        if (num == 0 || num == 1)
            return 1;
        else
            return num * findFactorial(num - 1);
    }

    public static void main(String[] args) {
        // Take input from the user
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number to calculate its factorial:");
        int num = sc.nextInt(); // Input the number

        if (num >= 0) {
            // Call the recursive function to find the factorial
            int factorial = findFactorial(num);
            System.out.println("The factorial of " + num + " is: " + factorial);
        } else {
            System.out.println("Factorial not possible for negative numbers.");
            System.out.println("Please enter a non-negative integer.");
        }
    }
}


Output :

Enter the number to calculate its factorial:
5
The factorial of 5 is: 120

Thanks for reading the post. If you have faced any issue in the post, please do comment. Because of your comment, we can improve the content and provide the best material in the future. So please like and share the post.

Recommended Post

03 comments on “Factorial Program in Java

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!