Monday, 7 December 2020

Factorial of Number using Method

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

Output:

Enter Number...
5

 Factorial of 5 is = 120

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32