factorial program in java

Factorial Program in Java

Factorial related programs are very common in the programming world. These types of programs are very important and useful. Because it helps to improve mindset and programming skills. Before writing the Factorial Program in Java, we need to understand the meaning of Factorial.

What is Factorial?

Basically, we find a factorial of a particular number. We can write a factorial of numbers like n!. “n” can be any positive number. For Better understanding please look below examples.
Example: n! = n*(n-1)*…….*1 .
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
In the above example, Factorial of 5 is 120. But in this tutorial, we will calculate factorial using Java Programming. But factorial of 0 (Zero) is always 1(One).

Factorial Program in Java

In Java Programming, we can write a program in the following ways.
1.) Factorial of a Number using For Loop.
2.) Factorial Program using While Loop.
3.) Factorial Program using Do-While Loop.
4.) Find Factorial of a number using recursion in java.
5.) Find Factorial of a number entered by the user in java.

Factorial Program in Java using For Loop

Factorial programs can be done in many ways. Because Looing is the main key for calculating the factorial of any number. In the below example, factorial will be calculated using for loop in java.

class FactorialPractice {

int fact_value=1;
int findFact=5; // Find factorial of this number
void findFactorial(){
       for (int i=1 ; i<=findFact ; i++){
            fact_value=fact_value*i;
       }
System.out.println("Factorial of a using For Loop is: "+fact_value);
}
}
// Main Class
class MyFactorialExample{
	public static void main(String...s){
	FactorialPractice factObj=new FactorialPractice();
	factObj.findFactorial();
	}
}

Output: Factorial of a using For Loop is: 120

Factorial Program in Java using While Loop

In this example, we will use while loop to find factorial. But the result will be the same.

class FactorialPractice {

int fact_value=1;
int findFact=5; // Find factorial of this number
int i=1;
void findFactorial(){
       while(i<=findFact){
            fact_value=fact_value*i;
            i++;
       }
System.out.println("Factorial of a using For Loop is: "+fact_value);
}
}
// Main Class
class MyFactorialExample{
	public static void main(String...s){
	FactorialPractice factObj=new FactorialPractice();
	factObj.findFactorial();
	}
}

Output: Factorial of a using For Loop is: 120

Factorial Program in Java using Do While Loop

Factorial programs can be done in many ways. Because Looing is the main key for calculating the factorial of any number. In the below example, factorial will be calculate using do-while Loop.

class FactorialPractice {

int fact_value=1;
int findFact=5; // Find factorial of this number
int i=1;
void findFactorial(){
       do{
          fact_value=fact_value*i;
          i++;
       }
       while(i<=findFact);          
System.out.println("Factorial of a using For Loop is: "+fact_value);
}
}
// Main Class
class MyFactorialExample{
	public static void main(String...s){
	FactorialPractice factObj=new FactorialPractice();
	factObj.findFactorial();
	}
}

Output: Factorial of a using For Loop is: 120

Factorial Program in Java using Recursion

Recursion is a very interesting concept in Java. It is processed, when the function calls itself again and again. Factorial can also be found using recursion. Because of recursion, code can be efficient and easy.

class FactorialPractice {
int findFactorial(int n){
	if (n == 0){ 
          return 1; 
	} 
        return n*findFactorial(n-1);
}
}
// Main Class
class MyFactorialExample{
	public static void main(String...s){
	FactorialPractice factObj=new FactorialPractice();
        //Give number as an argument.
	System.out.println("Factorial of number:"+factObj.findFactorial(5));
	}
}

Output: Factorial of number: 120

Thanks for reading the post. If you have faced any issue in the post, please do comment. Because of your comment, we can improve the content and provide the best material in the future. So please like and share the post.

Recommended Post

Share and Enjoy !

0Shares
0 0 0

02 comments on “Factorial Program in Java

Leave a comment

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

0 0 0
FOLLOW ON