Day 9 :Recursion
Solution In Java 8:
import java.io.*;import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ans;
int n = sc.nextInt();
ans = factorial(n);
System.out.println(ans);
}
public static int factorial(int n){
if(n == 1){
return 1;
}
else{
return (n * factorial(n-1));
}
}
}
Solution In Python 3:
n = int(input().strip())
def factorial(n):
if n is 1:
return 1
else:
return (n) * factorial(n-1)
ans = factorial(n)
print(ans)
0 comments:
Post a Comment