Skip to content

A method to calculate the factorial of a non-negative integer

Notifications You must be signed in to change notification settings

Aleangelozi/factorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

factorial

A method to calculate the factorial of a non-negative integer.

Briefly, a factorial of a non-negative integer n, denoted by n!, is expressed by the math expression: n! = n(n - 1)(n - 2)(n - 2)∙ ... ∙1.

An extended definition includes: 1! = 1 and 0! = 1

For example, 5! = 5 x 4 x 3 x 2 x 1 = 120.

#Java#

public class Factorial {

public static int calculateFactorial(int n){
	
	for (int i = n; i >= 2 ;){
        n *= --i;
	}    
	return n;
	
}
public static void main(String[] args) {
	System.out.println(calculateFactorial(5));
}

}

#JavaScript#

function factorial (n) {

for (var i = n; i >= 2 ;){ n *= --i; }
return n; } factorial(5)

About

A method to calculate the factorial of a non-negative integer

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published