How to Print Factorial of a Number Using While Loop in Java

When it comes to programming in Java, understanding loops is crucial. In this tutorial, we’ll explore how to calculate and print the factorial of a number using a while loop in Java. Factorials are fundamental mathematical operations, and learning how to implement them in Java will help you grasp the concept of loops and iterative programming.

How to Print Factorial of a Number Using While Loop in Java

Calculating Factorials with a While Loop

To calculate the factorial of a number using a while loop in Java, you’ll need to follow these steps:

Step 1: Define Variables

Before we start, let’s declare the variables we need. We’ll need an integer variable to store the number for which we want to find the factorial and another integer to store the result.

import java.util.Scanner;

public class FactorialCalculator {
    public static void main(String[] args) {
        int num, factorial;
        factorial = 1; // Initialize the factorial to 1
        // Create a Scanner object for user input
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a number: ");
        num = scanner.nextInt();

Step 2: Calculate Factorial Using a While Loop

Next, we’ll use a while loop to calculate the factorial. A factorial of a non-negative integer ‘n’ (denoted as ‘n!’) is the product of all positive integers from 1 to ‘n’. We’ll start with ‘factorial’ initialized to 1 and multiply it by ‘num’ in each iteration, reducing ‘num’ by 1 until ‘num’ becomes 1.

while (num > 1) {
            factorial *= num;
            num--;
        }

Step 3: Display the Result

Finally, we’ll print the calculated factorial to the console.

System.out.println("Factorial is: " + factorial);
    }
}

Full Code Listing

import java.util.Scanner;

public class FactorialCalculator {
    public static void main(String[] args) {
        int num, factorial;
        factorial = 1; // Initialize the factorial to 1
        // Create a Scanner object for user input
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a number: ");
        num = scanner.nextInt();
        
        while (num > 1) {
            factorial *= num;
            num--;
        }
        
        System.out.println("Factorial is: " + factorial);
    }
}

Explanation

  • We start by defining two integer variables, ‘num’ and ‘factorial’. ‘num’ will store the number for which we want to calculate the factorial, and ‘factorial’ will store the result, initialized to 1.
  • We create a Scanner object to get user input for the number.
  • Using a while loop, we calculate the factorial of ‘num’. The loop continues until ‘num’ becomes 1. In each iteration, we multiply ‘factorial’ by ‘num’ and decrement ‘num’ by 1.
  • Finally, we display the calculated factorial on the console.

Conclusion

In this tutorial, we’ve learned how to calculate and print the factorial of a number using a while loop in Java. Understanding this fundamental programming concept is essential for anyone looking to delve deeper into Java and programming in general. Loops, like the while loop, are powerful tools for automating repetitive tasks and solving a wide range of problems in software development. By mastering them, you’ll be better equipped to tackle more complex challenges in Java and other programming languages. Practice this program and experiment with different numbers to gain a deeper understanding of how while loops work and how factorials are calculated.

Leave a Comment