Day 9: Recursion 3 HackerRank Solution


Day 9: Recursion 3 HackerRank Solution
Source : https://www.hackerrank.com/challenges/30-recursion



Source : https://www.hackerrank.com/challenges/30-recursion


Solution


// Karthikalapati.blogspot.com
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
System.out.println(factorial(n));
}
private static Long factorial (int n) {
if (n < 0) {
return null;
}
long result = 1;
while (n > 0) {
result *= n--;
}
return result;
}
}

No comments:

Post a Comment