forked from bishweashwarsukla/hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factorial_LinkedList.cpp
101 lines (95 loc) · 1.51 KB
/
Factorial_LinkedList.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
#include<stdlib.h>
#include<sstream>
using namespace std;
struct final
{
int a;
struct final* next;
};
struct final* head = NULL;
struct final* tail = head;
void insert(int a)
{
struct final* temp = (struct final*)malloc(sizeof(struct final));
temp->a = (a % 10);
a = a / 10;
temp->next = head;
head = temp;
tail = temp;
while(a != 0)
{
struct final* temp = (struct final*)malloc(sizeof(struct final));
temp->a = (a % 10);
a = a / 10;
tail->next = temp;
temp->next = NULL;
tail = temp;
}
}
void display()
{
struct final* ptr;
ptr = head;
stringstream ss;
ss<<head->a;
string s,b;
ss>>s;
ptr = ptr->next;
while(ptr != NULL)
{
stringstream ss;
ss<<ptr->a;
ss>>b;
s = s + b;
ptr = ptr->next;
}
int i;
for(i = s.length() - 1; i >= 0; i--)
printf("%c",s[i]);
}
void multiply(int b)
{
int c;
c = 0;
struct final* ptr;
ptr = head;
while(ptr != NULL)
{
ptr->a = (ptr->a * b) + c;
c = (ptr->a / 10);
ptr->a = ptr->a - c * 10;
ptr = ptr->next;
}
if(ptr == NULL && c != 0)
{
while(c != 0)
{
struct final* temp = (struct final*)malloc(sizeof(struct final));
temp->a = (c % 10);
c = c / 10;
tail->next = temp;
tail = temp;
tail->next = NULL;
}
}
}
int main()
{
int T, N;
scanf("%d", &T);
while(T--)
{
scanf("%d", &N);
insert(N--);
while(N != 0)
{
multiply(N);
N--;
}
display();
printf("\n");
head = NULL;
}
return 0;
}