Java program to print the factorial of the given number


Factorial of a positive integer n is the product of all values from n to 1. For example, the factorial of 3 is (3 * 2 * 1 = 6). 

Algorithm

1. Take integer variable A
2. Assign a value to the variable
3. From value, A up to 1 multiply each digit and store
4. The final stored value is factorial of A

Example

import java.util.Scanner;
   public class Factorial {
      public static void main(String args[]){
         int i, factorial=1, number;
         System.out.println("Enter the number to which you need to find the factorial:");
         Scanner sc = new Scanner(System.in);
         number = sc.nextInt();

         for(i = 1; i<=number; i++) {
            factorial = factorial * i;
         }
         System.out.println("Factorial of the given number is:: "+factorial);
      }
   }

Output

Enter the number to which you need to find the factorial:
25
Factorial of the given number is:: 2076180480

Updated on: 13-Mar-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements