-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFactorial.java
More file actions
25 lines (25 loc) · 808 Bytes
/
Factorial.java
File metadata and controls
25 lines (25 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner;
/**
*Factorial program takes an number from the user and calculates the factorial using tail recursion.
*/
public class Factorial {
public static void main (String args[]) {
int getNum;
System.out.println("Enter the number to calculate factorial for:");
Scanner num = new Scanner(System.in);
getNum = num.nextInt();
int factorialVal = factorial(getNum);
System.out.println("Factorial is: "+ factorialVal);
}
/**
*factorial method recursively calculates the factorial.
*@param number the number for which factorial value is calculated
*@return the factorial calculated for the parameter number.
*/
public static int factorial (int number){
if (number == 0)
return 1; //base condition
else
return number*factorial(number-1); //recursive call
}
}