Table of Contents
Java program to find factorial of a number
In this tutorial, we will discuss the Java program to find factorial of a number|in 6 ways
In this post, we are going to learn how to find the factorial of a number or the given number in Java language
What is the factorial of a number (n)?
The factorial of a number (n) is the product of all positive integers from 1 up to n (n is the given number).
It is simply denoted by n!.
Example
if you want to find the factorial for number 5, you can follow this method.
Factorial of 5 will be 5*4*3*2*1=120
So, 5!=120.
Java program to find factorial of a number
Code to find factorial – using the stranded method
Program 1
class Factorial_Calc1 { public static void main(String args[]){ int num=5,fact=1; for(int i=1; i<=num; i++){ fact=fact*i; } System.out.println("Factorial of "+num+" is: "+fact); } }
When the above code is executed it produces the following output
Factorial of 5 is: 120
In this program
- Integer variable num,fact are declared and initialized
- the program finds the factorial of a number using for loop
- Then, the program is displayed the factorial of a number using System.out.println()
Code to find factorial – using the for loop
Program 2
import java.util.Scanner; class Factorial_Calc2 { public static void main(String args[]){ int num,fact=1; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for find factorial: "); num=scan.nextInt();//get input from the user for num1 for(int i=1; i<=num; i++){ fact=fact*i; } System.out.println("Factorial of "+num+" is: "+fact); } }
When the above code is executed it produces the following output
Enter the number for find factorial: 6 Factorial of 6 is: 720
The program allows the user to enter a value and it finds and displays factorial of the given number using the for loop in Java language
Code to find factorial – using the while loop
Program 3
import java.util.Scanner; class Factorial_Calc3 { public static void main(String args[]){ int num,fact=1; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for find factorial: "); num=scan.nextInt();//get input from the user for num1 int i=1; while( i<=num){ fact=fact*i; i++; } System.out.println("Factorial of "+num+" is: "+fact); } }
When the above code is executed it produces the following output
Enter the number for find factorial:5 Factorial of m5 is:120
The program allows the user to enter a value and it finds and displays factorial of the given number using while loop in Java language
Code to find factorial – using the do-while loop
Program 4
import java.util.Scanner; class Factorial_Calc4 { public static void main(String args[]){ int num,fact=1; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for find factorial: "); num=scan.nextInt();//get input from the user for num1 int i=1; do{ fact=fact*i; i++; }while( i<=num); System.out.println("Factorial of "+num+" is: "+fact); } }
When the above code is executed it produces the following output
Enter the number for find factorial: 7 Factorial of 7 is: 5040
The program allows the user to enter a value and it finds and displays factorial of the given number using do-while loop Java language
Code to find factorial – using the method
Program 5
import java.util.Scanner; class Factorial_Calc5 { public static void main(String args[]){ int num; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for find factorial: "); num=scan.nextInt();//get input from the user for num1 int result=factorial(num); // System.out.println("Factorial of "+num+" is: "+result); } static int factorial(int n){//user defined method int i=1,fact=1; for(i=1; i<=n; i++){ fact=fact*i; } return fact; } }
When the above code is executed it produces the following output
Enter the number to find factorial:6 Factorial of 6 is:720
The program allows the user to enter a value and it finds and displays factorial of the given number using the user-defined method in java language
Code to find factorial – using recursion
Program 6
import java.util.Scanner; class Factorial_Calc6 { public static void main(String args[]){ int num; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for find factorial: "); num=scan.nextInt();//get input from the user for num1 long result=factorial(num); // System.out.println("Factorial of "+num+" is: "+result); } public static long factorial(int num){//recursive method if(num>=1) return num * factorial(num-1); else return 1; } }
When the above code is executed it produces the following output
Enter the number for find factorial: 8 factorial of 8 is: 40320
The program allows the user to enter a value and it finds and displays factorial of the given number using the recursive method in Java language
Suggested for you
Similar post
C program to find factorial of a number
Java program to find factorial of a number
Python program to find factorial of a number
C++ program to find factorial of a number