Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Factorial
- Given n of 1 or more, return the factorial of n, which is n * (n-1) * (n-2) ... 1. Compute the result recursively (without loops).
- Input
- On the first line you will be given n.
- Output
- On the only output line you should print F(n).
- Constraints
- n>1
- Sample tests
- Input
- 1
- Output
- 1
- Input
- 5
- Output
- 120
- import java.util.Scanner;
- public class Factoriel {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- System.out.println(factoriel(n));
- }
- public static int factoriel(int n){
- if(n<=1){
- return 1;
- }
- return n * factoriel(n-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment