import java.util.Scanner;
class Factorial {
public static void main(String args[]) {
int n, c, fact = 1;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else {
for (c = 1; c <= n; c++) fact = fact * c;
System.out.println("Factorial of " + n + " is = " + fact);
}
}
}
//Calculate factorial for large No
import java.util.Scanner;
import java.math.BigInteger;
class BigFactorial {
public static void main(String args[]) {
int n, c;
BigInteger inc = new BigInteger("1");
BigInteger fact = new BigInteger("1");
Scanner input = new Scanner(System.in);
System.out.println("Input an integer");
n = input.nextInt();
for (c = 1; c <= n; c++) {
fact = fact.multiply(inc);
inc = inc.add(BigInteger.ONE);
}
System.out.println(n + "! = " + fact);
}
}
Let’s have both programs presented. The first one calculates the factorial for an integer using a simple loop in case the number is not too large, and the second one, which is pretty much the same computation, uses the BigInteger class.
Step-by-Step Explanation for Factorial Class:
1. Import Scanner Class:
It imports the Scanner class from java.util to allow the program to accept a user’s input.
2. Declare Class:
class Factorial {
// The Factorial class declares all the logic to be used for calculating the factorial.
3. Declaration of Main Method:
public static void main(String args[]) {
This is the point where the program begins running.
4. Variable Declarators
int n, c, fact =1;
- n: Number on whose factorial shall be calculated.
- c: counting variable.
- fact: Factorial value: This value is defined initially as 1.
5. Obtain a value input by the user :
Scanner in = new Scanner(System.in);
- The program prompts the user for an input.
- A Scanner Object is declared and used to scan from the keyboard.
6. Scan the Value of n:
n = in.nextInt();
The integer typed into the keyboard will be stored inside the variable n.
7. Check for a Negative Input:
if (n < 0)
System.out.println(“Number should be non-negative.”);
- It determines whether the input is negative. If n is negative, it issues an error message.
- Factorials are not defined for negative numbers.
8. Factorial Computation with a Loop:
else {
for (c = 1; c <= n; c++) {
fact = fact * c;
}}
System.out.println(“Factorial of ” + n + ” is = ” + fact);
If the number is nonnegative, a for loop is used to calculate the factorial by multiplying fact with each integer from 1 to n.
9. Print the Answer:
Once the loop is done, the program prints out the calculated factorial.
Execution Example
Input:
Enter an integer to find its factorial
5
The program calculates 5! = 5 * 4 * 3 * 2 * 1 = 120.
Output:
Factorial of 5 is = 120
Step-by-step Explanation for BigFactorial Class:
1. Import Required Classes:
- Scanner to obtain the user’s input.
- BigInteger, as the calculations are too huge for large input.
- BigInteger is used since the factorials tend to increase so fast that cannot be stored inside int or long primitives.
2. Class declaration:
class BigFactorial {
The BigFactorial class enables us to calculate the factorials of big integer numbers
3. Main Method Definition:
public static void main(String args[]) {
4. Declarations of Variables and Objects:
int n, c;
BigInteger inc = new BigInteger(“1”);
BigInteger fact = new BigInteger(“1”);
- n: the value for which will be computed as factorial.
- c : the loop counter of.
- inc: BigInteger initialized to 1 used for multiplication with fact and to increase by 1 in each cycle.
- fact: BigInteger initialized to 1 where the result of factorial will be stored.
5. Declare Scanner Object for Input Reading
Scanner input = new Scanner(System.in);
This declares a scanner object that reads the input from the user.
6. Ask for Input from User
System.out.println(“Input an integer”);
This line asks the user to input a number for which the factorial is to be calculated.
7. Input the Value of n:
n = input.nextInt();
8. Factorial Calculation Using BigInteger:
for (c = 1; c <= n; c++) {
fact = fact.multiply(inc); // Multiply fact by inc
inc = inc.add(BigInteger.ONE); // Increment inc by 1
}
A for loop runs from 1 to n. In each iteration:
fact is multiplied by inc.
inc is added 1 to it using add of BigInteger.
9. Print the Answer
System.out.println(n + “!” + ” = ” + fact);
The program prints the computed factorial using BigInteger.
Running Example for BigFactorial
Input:
Input an integer
20
Running:
It computes 20! that is a number that is quite huge:
20! = 20 * 19 * 18 *. * 1 = 2432902008176640000.
Output
20! = 2432902008176640000
Summary of Output for Both Programs:
1. For Factorial Program with input 5:
Factorial of 5 is = 120
2. For BigFactorial Program with input 20:
20! = 2432902008176640000
Conclusion:
- The first program calculates the factorial for smaller numbers using a simple for loop with an integer.
- The second program uses the BigInteger class to calculate the factorial of large numbers, which is essential as factorials grow very fast and exceed the limits of primitive data types.
- Both examples show how both programs handle a simple user’s input and compute logic to evaluate the factorial for a given integer.