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
Factorial of nth number

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

Java Operator

Java Data type

Java if statements

Java for loop

Java while loop

Java do-.while loop

Java methods

Calculate Electricity bill using Java method
Cpp program to find factorial of a number
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago