Monday, 7 December 2020

Factorial of Number using Recursion

import java.util.*;
public class FactorialRecursion 
{
	int fact(int n)
	{
		if(n==0)
			return 1;
		else
			return(n*fact(n-1));
	}
	public static void main(String[] args) 
	{
		FactorialRecursion obj = new FactorialRecursion();
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1;
		fact = obj.fact(number);
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:

Enter Number...
4

 Factorial of 4 is = 24

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32