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.

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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago