Site icon Codeforcoding

Program to calculate factorial of a number using recursion in Java

Program to calculate factorial of a number using recursion in Java

In this tutorial, we will discuss the Program to calculate factorial of a number using recursion in Java

There are many ways to calculate factorial using Java language. and one of this given below

 

In this article, we are going to learn how to calculate factorial of a number using the recursive method in Java language

Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one

 

 

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

Program

This program allows the user to enter a positive integer number and it calculates the factorial of the given number using the recursive method in Java language

import java.util.Scanner;
class FactorialRecursion{
public static void main(String args[]){
int num;//declare the variable num
    Scanner scan=new Scanner(System.in); 
    //create a scanner object for reacieve input
System.out.print("Enter the positive integer number: ");
num=scan.nextInt();//reads input from the user

long factorial=findFact(num);//call the function findFact();
System.out.println("Factorial of "+num+" : "+factorial);
//display factorial on the screen
}
public static long findFact(int num){//recursive function
    if(num>=1){
        return num*findFact(num-1);
        //recursive function called itself
    }
    else{
        return 1;
    }	
    }
}

The above code is executed, it produces the following result

Enter the positive integer number:6
Factorial of 6: 720


Approach

 

 

To clearly understand this article, you should have the previous knowledge of the following C programming subject

The recursive method in Java

The method in Java language

If statements in Java

 

Similar post

Find factorial of a number  in Java

Find factorial of a number  in C language

Find factorial of a number  in C++ language

Find factorial of a number  in Python language

Find factorial of a number  in Java using method

Find factorial of a number  in C using the function

Find factorial of a number  in C++ using the function

Find factorial of a number  in Python using the function

Find factorial of a number  using the pointer in C language

Find factorial of a number  using the pointer in C++ language

Find factorial of a number  using the recursion in C++ language

Find factorial of a number  using the recursion in C language

 

 

 

 

 

 

Python program to multiply two numbers without using arithmetic operator
Program for calculating factorial of a number using recursion in C++
Exit mobile version