A Program That Takes Input And Tell it's Factorial without Using Any Loop(Java Program) - HelpMeOutAlways

Tuesday, February 19, 2019

A Program That Takes Input And Tell it's Factorial without Using Any Loop(Java Program)

Source Code

import java.util.Scanner;
class FactorialRecursion{
    public void fact(){

      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter the number:");

      int num = scanner.nextInt();

      int factorial = fact(num);
      System.out.println("Factorial of entered number is: "+factorial);
   }
   static int fact(int n)
   {
       int output;
       if(n==1){
         return 1;
       }

       output = fact(n-1)* n;
       return output;
   }
}
class Main{
   public static void main(String args[]){
     FactorialRecursion obj=new FactorialRecursion();
     obj.fact();
   }
}

Output

java program

No comments:

@way2themes