Java
Java

 

The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers less than or equal to n.
Factorials are widely used in mathematics, particularly in permutations, combinations, and other areas of combinatorial analysis.
Understanding how to compute the factorial of a number is a fundamental skill in programming, as it introduces concepts
such as recursion and iterative processing.

Objective

The objective of this program is to provide a simple Java implementation that calculates the factorial of a given
non-negative integer input by the user. This program will illustrate both iterative and recursive approaches to solve
the factorial calculation problem.

Java Code


        import java.util.Scanner;

        public class FactorialCalculator {
            // Method to calculate factorial iteratively
            public static long factorialIterative(int n) {
                long result = 1;
                for (int i = 1; i <= n; i++) {
                    result *= i;
                }
                return result;
            }

            // Method to calculate factorial recursively
            public static long factorialRecursive(int n) {
                if (n == 0) {
                    return 1;
                }
                return n * factorialRecursive(n - 1);
            }

            public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter a non-negative integer: ");
                int number = scanner.nextInt();

                if (number < 0) {
                    System.out.println("Factorial is not defined for negative integers.");
                } else {
                    long iterativeResult = factorialIterative(number);
                    long recursiveResult = factorialRecursive(number);
                    System.out.println("Factorial of " + number + " (Iterative): " + iterativeResult);
                    System.out.println("Factorial of " + number + " (Recursive): " + recursiveResult);
                }

                scanner.close();
            }
        }
        

Program Structure and Execution

The program consists of a class named FactorialCalculator that contains two methods for calculating factorials:
factorialIterative and factorialRecursive. The main method is where the program execution begins.
Here’s a brief overview of its structure:

  • factorialIterative(int n): This method computes the factorial of a number using a loop.
  • factorialRecursive(int n): This method computes the factorial using recursion.
  • main(String[] args): This is the entry point of the program. It prompts the user for input,
    checks if the input is valid, and then calls both methods to display the results.

How to Run the Program

  1. Ensure you have Java Development Kit (JDK) installed on your computer.
  2. Open a text editor or Integrated Development Environment (IDE) and create a new file named FactorialCalculator.java.
  3. Copy and paste the provided Java code into the file.
  4. Open a terminal or command prompt.
  5. Navigate to the directory where the file is saved.
  6. Compile the program using the command: javac FactorialCalculator.java.
  7. Run the program using the command: java FactorialCalculator.
  8. Follow the prompts to enter a non-negative integer and see the factorial computed by both methods.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)