Skip to content

Commit eaf4b59

Browse files
Comments added
1 parent fde50e6 commit eaf4b59

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Misc/LargeFactorial.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
class LargeFactorial
44
{
55
public static void main(String []args)
6-
{
6+
{
7+
System.out.print("Enter the number whose factorial you want to calculate: ");
78
Scanner in=new Scanner(System.in);
89
int n=in.nextInt();
910
int x,carry=0;
1011
Stack a=new Stack();
1112
Stack b=new Stack();
12-
a.push(1);
13-
for(int i=2;i<=n;i++)
13+
a.push(1);//Initialize stack with value '1'
14+
for(int i=2;i<=n;i++)//Running loop only for n>1 else give output '1'
1415
{
15-
while(!a.isEmpty()){
16+
while(!a.isEmpty())//Loop will run until stack a is not empty
17+
{
1618
x=(int)a.pop();
1719
b.push((x*i+carry)%10);
20+
//Getting digit from stack a and multipling with i and pushing its unit digit to stack b
21+
// And storing its carry that can be added to next position in stack
1822
carry=(x*i+carry)/10;}
23+
//Now if any carry is left to be added to sstack will be now added this step is the main step increasing the nnumber of digit in the number
1924
while(carry!=0)
2025
{
2126
b.push(carry%10);
@@ -24,9 +29,12 @@ public static void main(String []args)
2429
while(!b.isEmpty())
2530
a.push(b.pop());
2631
}
32+
33+
//Stack a containg Least significant digit on the top and Most significant Digit on the bottom so reversing it in stack b to print the result
2734
while(!a.isEmpty())
2835
b.push(a.pop());
2936
while(!b.isEmpty())
37+
//Printing the content of stack b
3038
System.out.print((int)b.pop());
3139
}
3240
}

0 commit comments

Comments
 (0)