-
Notifications
You must be signed in to change notification settings - Fork 9
/
addnum.cpp
156 lines (156 loc) · 2.73 KB
/
addnum.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<iostream>
#include<conio.h>
using namespace std;
int c = 0, c1 = 0;
struct node1
{
node1 *link;
int data1;
}*head = NULL, *m = NULL, *np1 = NULL, *ptr = NULL;
struct node
{
node *next;
int data;
}*start = NULL, *p = NULL, *np = NULL;
void store(int x)
{
np1 = new node1;
np1->data1 = x;
np1->link = NULL;
if (c == 0)
{
head = np1;
m = head;
m->link = NULL;
c++;
}
else
{
m = head;
while (m->link != NULL)
{
m = m->link;
}
m->link = np1;
np1->link = NULL;
}
}
void keep(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if (c1 == 0)
{
start = np;
p = start;
p->next = NULL;
c1++;
}
else
{
p = start;
while (p->next != NULL)
{
p = p->next;
}
p->next = np;
np->next = NULL;
}
}
void add()
{
int i = 0;
node1 *t = head;
node *v = start;
while (t != NULL)
{
if (v == NULL)
{
t->data1 = t->data1 + i;
i = t->data1 / 10;
t->data1 = t->data1 % 10;
if (t->link == NULL && i == 1)
{
ptr = new node1;
ptr->data1 = i;
ptr->link = NULL;
t->link = ptr;
t = t->link;
}
t = t->link;
continue;
}
else
{
t->data1 = t->data1 + v->data + i;
i = t->data1 / 10;
t->data1 = t->data1 % 10;
if (t->link == NULL && i == 1)
{
ptr = new node1;
ptr->data1 = i;
ptr->link = NULL;
t->link = ptr;
t = t->link;
}
t = t->link;
v = v->next;
}
}
}
void traverse()
{
node1 *q = head;
int c = 0,i = 0;
while (q != NULL)
{
q = q->link;
c++;
}
q = head;
while (i != c)
{
x[c - i - 1] = q->data1;
i++;
q = q->link;
}
cout<<"Result of addition for two numbers:";
for (i = 0; i < c; i++)
{
cout<<x[i]<<"\t";
}
}
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int n, x, mod, mod1;
cout<<"Enter the two numbers"<<endl;
cin>>n;
cin>>x;
if (x > n)
{
swap(&x,&n);
}
while (n > 0)
{
mod = n % 10;
n = n / 10;
store(mod);
}
while (x > 0)
{
mod1 = x % 10;
x = x / 10;
keep(mod1);
}
add();
traverse();
getch();
}