-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #358 from vipulSVJ/fact
Added a factorial file using recursion in java
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import java.util.Scanner; | ||
|
||
public class recursiveFactorial { | ||
|
||
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; | ||
} | ||
} |