Java Program to find Factorial of a Number using Recursion - The Coding Shala
Home >> Java Programs >> Factorial Program using Recursion
Other Posts You May Like
In this post, we will learn how to find Factorial of a Number using Recursion in Java.
Java Program to find Factorial of a Number using Recursion
Factorial of a non-negative number is the multiplication of numbers from 1 to n [n including]. Factorial of n is represented by n!.
n! = 1 * 2 * 3 * 4........ * (n-1) * n
Example 1:
Input: 5
Output: 120
Explanation: 1 * 2 * 3 * 4 * 5 = 120
Java Program:
// Java program to Find Factorial using Recursion import java.util.Scanner; public class Main { public static long findFact(long num) { // break condition if(num <= 0) { return 1; } return num * findFact(num-1); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); // take number input System.out.println("Enter the number"); long num = sc.nextLong(); long fact = findFact(num); System.out.println("Factorial of " + num + " is: " + fact); sc.close(); } }
Output:
Enter the number 10 Factorial of 10 is: 3628800
- Java Program to find Factorial of a Number using Loop
- Java Program to Find the Area of Triangle
- Java Program to Find the Area of Circle
- Java Program to Find the Area of Rectangle
- Java Program to find Sum of Digits of a Number
Comments
Post a Comment