Factorial using recursion - Coder's point

Saturday, March 16, 2019

Factorial using recursion

 import java.util.*;  
 class Recursive_fact  
 {  
   int n;  
   int factorial(int n)  
   {  
     if(n==1)  
       return 1;  
     else  
       return n*factorial(n-1);  
   }  
   public static void main()  
   {  
     Recursive_fact obj= new Recursive_fact();  
     Scanner in=new Scanner(System.in);  
     System.out.println("Enter a number");  
     int n=in.nextInt();  
     int a=obj.factorial(n);  
     System.out.println("FACTORIAL "+a);  
   }  
 }  
OUTPUT:
Enter a number
6
FACTORIAL 720

No comments:

Post a Comment