Java Program to Calculate Factorial of a Number Using Recursion

You are currently viewing Java Program to Calculate Factorial of a Number Using Recursion

Java program to calculate factorial of a number using recursion

// Java program to calculate factorial of a 
// number using recursion

import java.util.*;

public class Main {
  public static long getFactorial(int num) {
    if (num == 1)
      return 1;
    return num * getFactorial(num - 1);
  }

  public static void main(String[] args) {
    Scanner X = new Scanner(System.in);
    int num = 0;
    long fact = 0;

    System.out.print("Enter Number: ");
    num = X.nextInt();

    fact = getFactorial(num);

    System.out.printf("Factorial is: " + fact);
  }
}

Output

Enter Number: 5
Factorial is: 120

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply