Suppose we have to add two very large number like for eg. (43653465365365+74365365436765).
we store both the numbers in two linked lists we start adding the content of each unit from right hand side and store in third linked list.
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
int a2[10],m=0,n=0,a1[10],x=0,y=0,r1[10],r2[10],i=0,j=0;
char c1[10],c2[10],b;
cout<<"enter 1st number";
cin.getline(c1,10);
while(c1[i]!='\0')
{
a1[i]=c1[i]-48;
i++;m++;
}
cout<<"\n enter 2nd number";
cin.getline(c2,10);
while(c2[j]!='\0')
{
a2[j]=c2[j]-48;
j++;n++; }
for(i=0;i<m;i++)
r1[m-i-1]=a1[i];
for(j=0;j<n;j++)
r2[n-j-1]=a2[j];
node *l1,*l2,*l3,*p,*q,*t3,*t1,*t2;
l1=new node;
l1->data=r1[0];
l1->next=NULL;
p=l1;
do
{
t1=new node;
t1->data=r1[++x];
t1->next=NULL;
l1->next=t1;
l1=t1;
}while(x<m-1);
l1=p;
l2=new node;
l2->data=r2[0];
l2->next=NULL;
q=l2;
do
{
t2=new node;
t2->data=r2[++y];
t2->next=NULL;
l2->next=t2;
l2=t2;
}while(y<n-1);
l2=q;
node *z;
int c,a=0;
int aa;
int carry=0;
l3=new node;
aa=(l1->data)+(l2->data);
if(aa<10)
l3->data=aa;
else{
l3->data=(aa%10);
carry=1;}
l3->next=NULL;
z=l3;
l1=l1->next;
l2=l2->next;
while((l1!=NULL)&&(l2!=NULL))
{
aa= (l1->data)+(l2->data)+carry;
t3=new node;
if(aa<10)
{
t3->data=aa;
carry=0;}
else
{
t3->data=aa%10;
carry=1;
}
t3->next=NULL;
l3->next=t3;
l1=l1->next;
l2=l2->next;
l3=t3;
}
while((l1!=NULL&&l2==NULL))
{
aa=((l1->data)+carry);
carry=0;
t3=new node;
if(aa<10)
t3->data=aa;
t3->next=NULL;
l3->next=t3;
l3=t3;
l1=l1->next;
}
while((l2!=NULL&&l1==NULL))
{
aa=((l2->data)+carry);
carry=0;
t3=new node;
if(aa<10)
t3->data=aa;
t3->next=NULL;
l3->next=t3;
l3=t3;
l2=l2->next;
}
l3=z;
int k=0,d[11];
while(l3!=NULL)
{
d[k++]=l3->data;
l3=l3->next;
}
cout<<"\n the addition of two large number is\n";
for(int s=0;s<k;s++)
{
cout<<d[k-s-1]<<" ";
}
return 0;
}
we store both the numbers in two linked lists we start adding the content of each unit from right hand side and store in third linked list.
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int data;
node *next;
};
int main()
{
int a2[10],m=0,n=0,a1[10],x=0,y=0,r1[10],r2[10],i=0,j=0;
char c1[10],c2[10],b;
cout<<"enter 1st number";
cin.getline(c1,10);
while(c1[i]!='\0')
{
a1[i]=c1[i]-48;
i++;m++;
}
cout<<"\n enter 2nd number";
cin.getline(c2,10);
while(c2[j]!='\0')
{
a2[j]=c2[j]-48;
j++;n++; }
for(i=0;i<m;i++)
r1[m-i-1]=a1[i];
for(j=0;j<n;j++)
r2[n-j-1]=a2[j];
node *l1,*l2,*l3,*p,*q,*t3,*t1,*t2;
l1=new node;
l1->data=r1[0];
l1->next=NULL;
p=l1;
do
{
t1=new node;
t1->data=r1[++x];
t1->next=NULL;
l1->next=t1;
l1=t1;
}while(x<m-1);
l1=p;
l2=new node;
l2->data=r2[0];
l2->next=NULL;
q=l2;
do
{
t2=new node;
t2->data=r2[++y];
t2->next=NULL;
l2->next=t2;
l2=t2;
}while(y<n-1);
l2=q;
node *z;
int c,a=0;
int aa;
int carry=0;
l3=new node;
aa=(l1->data)+(l2->data);
if(aa<10)
l3->data=aa;
else{
l3->data=(aa%10);
carry=1;}
l3->next=NULL;
z=l3;
l1=l1->next;
l2=l2->next;
while((l1!=NULL)&&(l2!=NULL))
{
aa= (l1->data)+(l2->data)+carry;
t3=new node;
if(aa<10)
{
t3->data=aa;
carry=0;}
else
{
t3->data=aa%10;
carry=1;
}
t3->next=NULL;
l3->next=t3;
l1=l1->next;
l2=l2->next;
l3=t3;
}
while((l1!=NULL&&l2==NULL))
{
aa=((l1->data)+carry);
carry=0;
t3=new node;
if(aa<10)
t3->data=aa;
t3->next=NULL;
l3->next=t3;
l3=t3;
l1=l1->next;
}
while((l2!=NULL&&l1==NULL))
{
aa=((l2->data)+carry);
carry=0;
t3=new node;
if(aa<10)
t3->data=aa;
t3->next=NULL;
l3->next=t3;
l3=t3;
l2=l2->next;
}
l3=z;
int k=0,d[11];
while(l3!=NULL)
{
d[k++]=l3->data;
l3=l3->next;
}
cout<<"\n the addition of two large number is\n";
for(int s=0;s<k;s++)
{
cout<<d[k-s-1]<<" ";
}
return 0;
}
No comments:
Post a Comment