Java Program to Find Factorial of a Number

In this post, you will learn how to find the factorial of a number using Java Programming language.

Java Program to find factorial of a number

The factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n.

Factorial of n = n! = n * (n-1) * (n-2) * (n-3) * . . . . . . * 3 * 2 * 1 = 1 * 2 * 3 * . . . . . . . * (n-2) * (n-1) * n

The important point to note is factorial of 0 is 1 and the factorial of negative numbers is not possible.

So, let’s see the code of Java Program to find the factorial of a number.

Java Program to Find Factorial of a Number

import java.util.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    
    if(num < 0){
        System.out.println("Factorial of negative numbers cannot be calculated.");
    } else if(num == 0){
        System.out.println("Factorial of 0 is 1.");
    }
    else{
        long factorial = 1;
    
        for(int i=num; i>1 ; i--){
            factorial *= i;
        }
    
        System.out.println("Factorial of "+num+" is "+factorial);
    }
    

  }
}

Output 1

7
Factorial of 7 is 5040

Output 2

0
Factorial of 0 is 1.

Output 3

-213
Factorial of negative numbers cannot be calculated.

How Does This Program Work ?

  Scanner sc = new Scanner(System.in);
  int num = sc.nextInt();
  long factorial = 1;

In this program, we have declared an int-type variable named num. We have also declared a long type variable to store the factorial value because factorial contains large numbers. We have used Scanner Class in java to take the input from the user.

If you want to learn more about taking user inputs in Java. Read this : Input in Java

    if(num < 0){
        System.out.println("Factorial of negative numbers cannot be calculated.");
    } else if(num == 0){
        System.out.println("Factorial of 0 is 1.");
    }

Now, first, we check the integer, whether it is greater than 0 or less than 0 or equals 0. If it is less than 0, then our program will display “Factorial of Negative Numbers Doesn’t Exist.” and if the number is 0, then we print “Factorial of 0 is 1.”

   else{
        long factorial = 1;
    
        for(int i=num; i>1 ; i--){
            factorial *= i;
        }
    
        System.out.println("Factorial of "+num+" is "+factorial);
    }

And if the integer is greater than 0, then the following logic will be used to find the factorial and we display the result using System.out.println() function.

Conclusion

I hope after going through this post, you understand how to find factorial of a number in Java Programming language and learned to code a Java Program to Find Factorial of a Number.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

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