BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

java program to find factorial of a given number using recursion

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

Here we will write programs to find out the factorial of a number using recursion.

Program 1:
Program will prompt user for the input number. Once user provide the input, the program will calculate the factorial for the provided input number.

/**
 * @author: BeginnersBook.com
 * @description: User would enter the 10 elements
 * and the program will store them into an array and 
 * will display the sum of them.
 */
import java.util.Scanner;
class FactorialDemo{
   public static void main(String args[]){
      //Scanner object for capturing the user input
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter the number:");
      //Stored the entered value in variable
      int num = scanner.nextInt();
      //Called the user defined function fact
      int factorial = fact(num);
      System.out.println("Factorial of entered number is: "+factorial);
   }
   static int fact(int n)
   {
       int output;
       if(n==1){
         return 1;
       }
       //Recursion: Function calling itself!!
       output = fact(n-1)* n;
       return output;
   }
}

Output:

Enter the number:
5
Factorial of entered number is: 120

Program 2:
If you do not want user intervention and simply want to specify the number in program itself then refer this example.

class FactorialDemo2{
   public static void main(String args[]){
      int factorial = fact(4);
      System.out.println("Factorial of 4 is: "+factorial);
   }
   static int fact(int n)
   {
       int output;
       if(n==1){
         return 1;
       }
       //Recursion: Function calling itself!!
       output = fact(n-1)* n;
       return output;
   }
}

Output:

Factorial of 4 is: 24

Top Related Articles:

  1. Java Program to Make a Calculator using Switch Case
  2. Java Program to Calculate average using Array
  3. Java program to sum the elements of an array
  4. Java Program to Find square root of a Number without sqrt
  5. Neon Number in Java with example

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Abdul Khlaliq says

    September 9, 2015 at 11:11 AM

    Awesome tutorial I like it so much

    Reply
  2. nagendrababu says

    September 29, 2015 at 7:53 AM

    please find this answer/program?
    find nearest factorial of a given number and print factorial or not? ex:if i enter 25 it will print not a factorial and it is nearest to 4 factorial i.e.,24,if i enter 24 it will print it is a factorial of 4

    Reply
  3. Rhoen Kerr says

    February 1, 2016 at 2:36 AM

    Great for beginners like myself

    I will be a great programmer one day

    Thanks

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap