Introduction
The factorial of a non-negative integer ( n ) is the product of all positive integers less than or equal to ( n ). Factorials are commonly used in permutations, combinations, and other mathematical calculations. This guide will show you how to create a Java program to calculate the factorial of a number using the Scanner class to accept user input.
Problem Statement
Create a Java program that:
- Takes an integer input from the user.
- Calculate the factorial of that number.
- Displays the factorial to the user.
Example 1:
- Input:
5 - Output:
120(Factorial of 5 is ( 5 \times 4 \times 3 \times 2 \times 1 = 120 ))
Example 2:
- Input:
7 - Output:
5040(Factorial of 7 is ( 7 \times 6 \times 5 \times 4 \times 3 \times 2 \times 1 = 5040 ))
Solution Steps
- Prompt the User for Input: Use the
Scannerclass to read an integer input from the user. - Calculate the Factorial: Use a loop to multiply the integers from 1 to the input number.
- Display the Result: Output the calculated factorial to the user.
Java Program
import java.util.Scanner;
/**
* Java Program to Calculate Factorial using Scanner
* Author: https://www.javaguides.net/
*/
public class FactorialCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Prompt the user for input
System.out.print("Enter a number to calculate its factorial: ");
int number = scanner.nextInt();
// Step 2: Calculate the factorial
long factorial = 1; // Factorial of 0 is 1
for (int i = 1; i <= number; i++) {
factorial *= i;
}
// Step 3: Display the result
System.out.println("Factorial of " + number + " is " + factorial);
}
}
Explanation
Step 1: Prompt the User for Input
- The program uses the
Scannerclass to read an integer input from the user. This input represents the number for which the factorial will be calculated.
Step 2: Calculate the Factorial
- A
forloop is used to calculate the factorial:- The loop starts from 1 and multiplies the current value of
factorialby the loop counteriuntilireaches the input number. - The variable
factorialis initialized to 1 because the factorial of 0 is defined as 1.
- The loop starts from 1 and multiplies the current value of
Step 3: Display the Result
- The calculated factorial is printed to the console using
System.out.println.
Output Examples
Example 1:
Enter a number to calculate its factorial: 5
Factorial of 5 is 120
Example 2:
Enter a number to calculate its factorial: 7
Factorial of 7 is 5040
Conclusion
This Java program calculates the factorial of a number entered by the user using the Scanner class. It employs a simple loop to perform the multiplication required to compute the factorial. This method is efficient and easy to understand, making it a useful tool for learning and applying basic Java concepts.
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment