PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION
PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION
I/P:
3
O/P:
6
SOLUTION:
package Numbers;
import java.util.*;
public class Factorial {
static int factorial(int n) {
return (n > 1) ? n * factorial(n-1) : 1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
int result = factorial(n);
System.out.println(result);
sc.close();
}
}
Comments
Post a Comment