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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

4 weeks ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

4 weeks ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.