JAVA program to find factorial of a given number using method

JAVA program to find factorial of a given number using method

This JAVA program is to find factorial of a given number using method.

For example, factorial of a given number(5) using method will be factorial(5) = 120.

Dry run of the program has been given here(click on the link) only additional part is the use of method.

As practice you can run the same program with the different ways of using methods.

If you yet need the dry run of the program or any other query, then kindly leave a comment in the comment box or mail me, I would be more than happy to help you.

Program

import java.util.*;

class mr3
{
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
    		System.out.println("Enter a number");
    		int no=sc.nextInt();
    		int factorial = fact(no);
    		System.out.println("FACTORIAL of "+no+" = "+factorial);
		//another way of calling a function
		//comment above two lines if you want to use this
    		//System.out.println("FACTORIAL of "+no+" = "+fact(no));
	}

	static int fact(int n)
	{
		int i,f=1;
    		for(i=1;i<=n;i++)
    		{
        		f=f*i;
    		}	
    		return f;
	}
}

Output

Share Me!