- import java.util.Scanner;
- public class FactorialRecursion {
- public static void main(String[] args) {
- Scanner myScanner=new Scanner(System.in);
- System.out.print("Enter a number : ");
- int n=myScanner.nextInt();
- System.out.print("Factorial of "+n+" is : "+factorial(n));
- }
- static int factorial(int n)
- {
- int f;
- if(n==0){
- f=1;
- }else{
- f=n*factorial(n-1);
- }
- return f;
- }
- }
- //Enter a number : 5
- //Factorial of 5 is : 120
No comments:
Post a Comment