Factorial Program in Java using Scanner

Factorial Program in Java using Scanner | In mathematics, the factorial of a positive integer number specifies a product of all integers from 1 to that number. It is defined by the symbol exclamation mark (!).

Formula:- factorial(n) = n * factorial(n-1)

General Case for Finding Factorial

Factorial (n) =
1 * 2 * … * (n-1) * n
Or,
n * (n-1) * … * 2 * 1

Note:- The factorial of a negative number and float number doesn’t exist.

The Base Case for Finding Factorial,

factorial(0) = 1
Or,
factorial(1) = 1

For example:- The factorial of 5 = 5! = 54321 or 12345 = 120

Java Program to Find Factorial of a Number using Scanner

In this program, we will discuss how to find the factorial of a number using the For Loop.

1) Take an integer number
2) Declare a temporary variable fact and initialize it with 1. long fact = 1;
3) Take an iterator variable i, starting from 1
4) Multiply the fact variable and iterator variable. Store the result into the fact variable. fact = fact * i;
5) Increase the iterator variable by 1.
6) Repeat 4 and 5 steps until the iterator variable is less than the given number.

import java.util.Scanner;
public class Factorial {

  public static long findFactorial(int number){

     // declare variable
     long fact = 1;

     for(int i=1; i<=number; i++) {
       fact = fact * i;
     }

     return fact;
  }

  public static void main(String[] args) {

     // declare variables
     int number = 0;
     long result = 0;

     //create Scanner class object
     // to take input
     Scanner scan = new Scanner(System.in);

     // take input from end-user
     System.out.print("Enter integer number: ");
     number = scan.nextInt();

     // Find the factorial of 
     // the given number
     result = findFactorial(number);

     // display result
     System.out.println("Factorial = "+ result);

     // close Scanner class object
     scan.close();
  }
}

Output for the input values test-case-1:-

Enter integer number: 3
Factorial = 6

Output for the input values test-case-2:-

Enter integer number: 6
Factorial = 720

This program calls the findFactorial() method to calculate the factorial value. The findFactorial method is used for loop and increment operators. The for loop calculates the factorial of a number using the increment operator.

We can also find the factorial of a number using the for loop and decrement operator.

public static long findFactorial(int number) {
     // declare variable
     long fact = 1;
     for(int i=number; i>1; i--) {
       fact = fact * i;
     }
     return fact;
}

In the previous program, we used the For Loop to find factorial of a number but in this program, we will use the While Loop to find factorial in java.

import java.util.Scanner;
public class Factorial {

  public static long findFactorial(int number) {
     // declare variable
     long fact = 1;
     int i = 1;

     while(i<=number) {
          fact = fact * i;
          i++;
     }
     return fact;
  }

  public static void main(String[] args) {

     // declare variables
     int number = 0;
     long result = 0;

     //create Scanner class object
     Scanner scan = new Scanner(System.in);
     // take input from end-user
     System.out.print("Enter integer number: ");
     number = scan.nextInt();

     // Find factorial 
     result = findFactorial(number);

     // display result
     System.out.println("Factorial = "+ result);

     // close Scanner class object
     scan.close();
   }
}

Output for the input values test-case-1:-

Enter integer number: 8
Factorial = 40320

We can also find the factorial of a number using the while loop and decrement operator.

public static long findFactorial(int number) {
  // declare variable
  long fact = 1;
  int i = number;
  // loop to repeat the process
  while(i>1) {
    fact = fact * i;
    i--;
  }
  return fact;
}

Factorial Program in Java using Scanner

Here, we will develop a program to find the factorial for large values. To store the factorial value instead of int data type we are using a variable of long data type because the factorial value will be a larger number. But for a large number (like 50, 100) long data type is also not enough to store those values. So, we need to use a variable of BigInteger type which is defined in java.math package (not java.lang.Math class).

To multiply we can’t use the multiplication (*) operator because it is an object of BigInteger class so, we should use the multiply methods of BigInteger class.

import java.math.BigInteger;
import java.util.Scanner;
public class Factorial {

  public static BigInteger findFactorial(int number){

     // declare variable
     BigInteger fact = BigInteger.ONE;

     for(int i=1; i<=number; i++) {
       fact = fact.multiply(BigInteger.valueOf(i));
     }
     return fact;
  }

  public static void main(String[] args) {

     // declare variables
     int number = 0;

     //create Scanner class object
     // to take input
     Scanner scan = new Scanner(System.in);

     // take input from end-user
     System.out.print("Enter an integer number: ");
     number = scan.nextInt();

     // display result
     System.out.println("Factorial = "+ 
		        findFactorial(number));

     // close Scanner class object
     scan.close();
  }
}

Output for the input values test-case-1:-

Enter an integer number: 55
Factorial = 12696403353658275925965100847566516959580321051449436762275840000000000000

Output for the input values test-case-2:-

Enter an integer number: 60
Factorial = 8320987112741390144276341183223364380754172606361245952449277696409600000000000000

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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