What is Factorial ?
A factorial is a mathematical function denoted by an exclamation mark (!) that is used to calculate the product of all positive integers from 1 up to a given positive integer 'n'. It is defined as follows:
n! = n × (n - 1) × (n - 2) × ... × 3 × 2 × 1
For example:
- 5! = 5 × 4 × 3 × 2 × 1 = 120
- 4! = 4 × 3 × 2 × 1 = 24
- 3! = 3 × 2 × 1 = 6
- 2! = 2 × 1 = 2
- 1! = 1
It's important to note that by convention, 0! is defined to be 1.
Factorials are often used in mathematics and various fields of science to solve combinatorial problems, calculate permutations and combinations, and in the Taylor series expansion of mathematical functions, among other applications. They grow very quickly as 'n' increases, making them useful in situations involving large numbers of arrangements or possibilities.
Factorial value of integer number in java:
Algorithm of the program:
- Import the Scanner class to enable user input.
- Create a Scanner object named input to read user input.
- Prompt the user to enter a number and store it in the variable a.
- Store the original value of a in a separate variable b for later use.
- Check if the input number b is negative. If it is negative, print a message indicating that the factorial of a negative number does not exist.
- If the input is non-negative, initialize a variable x to 1. This variable will be used to store the factorial value, and we start with 1 as the initial factorial value.
- Use a while loop to calculate the factorial. In each iteration of the loop, multiply the current value of x by the value of b, and then decrement b by 1. Repeat this process until b becomes less than 1.
- After the while loop finishes, print the calculated factorial value.
- Close the Scanner to free up system resources.
- This code efficiently calculates the factorial of a non-negative integer entered by the user using a while loop and handles the case of a negative input.
Source code of the program:
import java.util.Scanner; public class Whileloop { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number , which want to get factorial value :"); int a = input.nextInt(); int b = a; if (b < 0) { System.out.print("Factorial of negative number does not exist ."); } else { int x = 1; while (b >= 1) { x = x * b; b--; } System.out.println("Factorial value of " + a + " is:" + x); } } }
Now, let's break down the code step by step:
- We begin by importing the Scanner class from the java.util package, which allows us to read user input.
- We create a Scanner object named input to read input from the user.
- A message is displayed to the user, prompting them to enter a number they want to calculate the factorial for.
- The user's input is read and stored in the variable a.
- The original value of a is preserved by storing it in a separate variable b.
- We check whether the value of b (the original user input) is negative using an if statement. If it is negative, we display a message indicating that the factorial of a negative number does not exist.
- If the input is non-negative (i.e., greater than or equal to 0), we initialize a variable x to 1. This variable will be used to store the factorial value, starting with 1 as the initial factorial value.
- We enter a while loop to calculate the factorial. The loop continues as long as b is greater than or equal to 1.
- Inside the loop, we update the x variable by multiplying it by the current value of b. This effectively calculates the factorial incrementally in each iteration.
- We decrement the value of b by 1 in each iteration to move towards the base case of 1.
- After the while loop completes, we have calculated the factorial of the original user input, and we print this value along with a message.
- Finally, we close the Scanner object to free up system resources.
This code effectively calculates the factorial of a non-negative integer using a while loop and provides appropriate handling for negative inputs.
Tags:
Java
Share: