-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfiniteaddition.cpp
116 lines (102 loc) · 1.99 KB
/
infiniteaddition.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
#include<iostream>
using namespace std;
struct node
{
int val;
node *link;
};
node *insertBeg (node *start, int value)
{
node *newnode = new node();
newnode ->val=value;
newnode ->link= start;
}
void *traverse (node *start)
{
while(start!=NULL)
{
cout<<start->val;
start=start->link;
}
}
node *insertEnd(node *start, int value)
{
if (start==NULL)
{ start= insertBeg(start,value);
return start;
}
else
{
node *bk= start;
node *newnode = new node();
newnode ->val=value;
newnode ->link= NULL;
while(start->link !=NULL)
{
start=start->link;
}
start->link= newnode;
return bk;
}
}
int main()
{
node *start1 = NULL;
node *start2 = NULL;
node *cstart1 = NULL;
node *cstart2 = NULL;
node *start3 =NULL;
int n;
char ch='y';
cout<<"enter the first numbers "<<endl;
do
{
cin>>n;
start1=insertBeg(start1,n);
cout<<"enter y to continue and n to stop"<<endl;
cin>>ch;
}
while(ch=='y');
cout<<"insert 2 no";
do
{
cin>>n;
start2=insertBeg(start2,n);
cout<<"enter y to continue and n to stop"<<endl;
cin>>ch;
}
while(ch=='y');
cstart1=start1;
cstart2=start2;
int carry=0,num,mod;
while(cstart1!=NULL && cstart2!=NULL)
{
num= cstart1->val + cstart2->val+carry;
mod= num%10;
carry= num/10;
start3=insertBeg(start3 ,mod);
cstart1=cstart1->link;
cstart2=cstart2->link;
}
while (cstart1==NULL && cstart2!=NULL)
{
num=cstart2->val+carry;
mod= num%10;
carry= num/10;
start3=insertBeg(start3 ,mod);
cstart2=cstart2->link;
}
while (cstart2==NULL && cstart1!=NULL)
{
num=cstart1->val+carry;
mod= num%10;
carry= num/10;
start3=insertBeg(start3 ,mod);
cstart1=cstart1->link;
}
if(carry>0)
{start3=insertBeg(start3,carry);
}
cout<<"addition is :";
traverse(start3);
return 0;}