Sponsored Links

November 24, 2017

Factorial By Recursion


import java.util.Scanner;

public class RecursionEx {
 //main
 public static void main(String args[]){
  Scanner temp = new Scanner(System.in);
  System.out.println("Enter number to find its factorial: ");
  long x = temp.nextLong();
  System.out.println(fact(x));
  
 }
 
 //fact
 public static long fact(long n){
  if(n<=1)
   return 1;
  else
   return n * fact(n-1);
 }
}

No comments:

Post a Comment