-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLarge Factorial.java
More file actions
50 lines (48 loc) · 1.66 KB
/
Copy pathLarge Factorial.java
File metadata and controls
50 lines (48 loc) · 1.66 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Factorial of very large numbers using stack
*/
package Stack;
import java.util.Scanner;
import java.util.Stack;
/**
* @author Sushil
* @Description This program computes factorial of big numbers using stack to store bits
*/
public class stackFact {
/**
* @param args
*/
public static void main(String[] args) {
/*
* Defining three different stacks
*/
Stack<Integer> result=new Stack<Integer>();
Stack<Integer> auxilary_result = new Stack<Integer>();
Stack<Integer> temp = new Stack<Integer>();
int n,i;
int carry = 0;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
result.push(1); //Initializing the result stack with 1
for(i=2;i<=n;i++) //Running loop from 2 to n
{
while(!result.isEmpty()) //Loop until result stack becomes empty
{
int prod=(i*result.pop())+carry; //Calculate product using top element of result and carry
auxilary_result.push(prod%10); //push back the most significant bit of product back into the array
carry=prod/10; //treat rest as carry
}
//When result stack has been processed successfully it is now time to push back the carry into the stack
while(carry>0) {
auxilary_result.push(carry%10); //pushing carry into the stack bit by bit
carry/=10;
}
while(!auxilary_result.isEmpty()) //transfer auxilary_result to result stack
result.push(auxilary_result.pop());
}
while(!result.isEmpty()) //Since result stack has our required answer but it is in reverse direction so we will reverse and store it in some other stack
temp.push(result.pop());
while(!temp.isEmpty()) //Print the final result
System.out.print(temp.pop());
}
}