Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

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

Posted on October 16, 2019October 16, 2019

Table of Contents

  • Java program to find factorial of a number
    • Java program to find factorial of a number
      • Code to find factorial – using the stranded method
      • Code to find factorial – using the for loop
      • Code to find factorial – using the while loop
      • Code to find factorial – using the do-while loop
      • Code to find factorial – using the method
      • Code to find factorial – using recursion
    • Related

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

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

 

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes