MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / FactorialN

Class FactorialN

Java/Recursion/FactorialN.java:2–33  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import java.util.*;
2class FactorialN
3{
4 public static int f; //Global variable
5 static void fct(int n) //Tail recursive version
6 {
7 if (n <= 1)
8 return;
9 f *= n;
10 fct(n-1); //All Set and done
11 }
12 /*
13 static int fct(int n) //Head recursive version
14 {
15 if (n <= 1)
16 return 1;
17 return (n * fct(n-1));
18 // this n, makes the function here head recursive, as the function
19 // will have to return to its parent to do the multiplication,
20 // hence tail elimination cant be used
21 }
22 */
23 public static void main(String args[])
24 {
25 Scanner I = new Scanner(System.in);
26 System.out.println("Enter A Number");
27 int n = I.nextInt();
28 f = 1;
29 fct(n);
30 System.out.println("Your factorial is: " + f);
31 I.close();
32 }
33}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected