basic

Java program to find factorial of a number|in 6 ways

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

Factorial calculation

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

  1. Integer variable num,fact are declared and initialized
  2. the program finds the factorial of a number using for loop
  3. 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

Class and main method in Java

Method in Java

For loop in Java

While loop in Java

Do-while loop in Java

 

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

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.