Java code to calculate factorial of a number
- Home
- Calculations
- Java code to calculate factorial of a number
- On
- By
- 0 Comment
- Categories: Calculations
Java code to calculate factorial of a number
Java code to calculate factorial of a number
In this tutorial, we will discuss the concept of Java code to calculate factorial
Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one
There are many ways to calculate factorial using Java language. some of them are described as below.
Java code to calculate factorial of a number
Example
factorial of 5 is
5!=5*4*3*2*1=120
factorial of 4 is
4!=4*3*2*1=24
factorial of n is
n!=n*(n-1)*....2*1
Find factorial using for loop
import java.util.Scanner;
class FindFactorial1{
public static void main (String args[]){
int num,i,factorial=1;//declare variables
Scanner obj=new Scanner(System.in);
System.out.println("Enter a number for find factorial: ");
num=obj.nextInt();//get input from user and stored num variable
if(num<0){//it Checked the number as positive or negative
System.out.println("Number must be positive: ");
}//if it is true, displays this error message
else{
for(i=1; i<=num; i++){//iterates the for loop to find factorial from 1 to num
factorial=factorial*i;
}
System.out.println("Factorial of "+num+ " is "+factorial);
}
}
}
When the above code is compiled and executed, it produces the following results
Factorial of 5 is 120
This program intakes input (positive integer) from the user and calculates the factorial of the given number using Forloop and displays the result.
Find factorial using the while loop
import java.util.Scanner; class FindFactorial2{ public static void main (String args[]){ int num,i,factorial=1;//declare variables Scanner obj=new Scanner(System.in); System.out.println("Enter a number for find factorial: "); num=obj.nextInt();//takes input from user if(num<0){ System.out.println("Number must be positive: "); } else{ i=1; while(i<=num){ factorial=factorial*i; i++; } System.out.println("Factorial of "+num+ " is "+factorial); } } }
When the above code is compiled and executed, it produces the following results
Factorial of 5 is 720
This program takes input (positive integer) from the user and calculates the factorial of the given number using While loop and displays the result.
Find factorial using the do-while loop
import java.util.Scanner; class FindFactDoWhile{ public static void main(String args[]){ int n, fact=1; Scanner scan=new Scanner(System.in); System.out.println("Enter number for find factorial"); n=scan.nextInt(); int i=1; do { fact=fact*i; i++; }while(i<=n); System.out.println("number "+n+" factorial is : "+fact); } }
When the above code is compiled and executed, it produces the following results
Factorial of 7 is 5040
This program takes input (positive integer) from the user and calculates the factorial of the given number using Do-while loop and displays the result.
Factorial program using Java recursion
class FactRecursion{ public static void main(String args[]){ FactRecursion obj=new FactRecursion(); System.out.println(obj.factNum(5)); } public static int factNum(int n){ if(n<=1){ return 1; } else{ return(n*factNum(n-1)); } } }
When the above code is compiled and executed, it produces the following results
120
Suggested for you