Function factorial (iterative) Learn programming Java

Lesson:

Functions


Exercise:

Function factorial (iterative)


Objetive:

Create an iterative (non-recursive) function to calculate the factorial of the number specified as parameter:

Console.Write ( Factorial (6) );

would display
720


Code:

public class Main
{
	public static float Factorial(int n)
	{
		int result = 1;

		for (int i = 1; i <= n; i++)
		{
			result *= i;
		}
		return result;
	}

	public static void main(String[] args)
	{
		System.out.print(Factorial(6));
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.