# How To Add two 64-bit Numbers in C Programming ?

+1 vote
178 views

I want to add two 64-bit or more than 64-bit numbers and print sum which is of 64-bit or more than 64-bit.

posted Mar 17, 2014

+1 vote

There is a way to add 64-bit numbers by using BIG datatype but it is not applicable in C Programming, so we can use Linked List to solve this problem.

``````typedef struct list
{
int digit;
struct list *next;
}number;

void main()
{
char a[500],b[500],c[500];
char i;

printf("\nEnter First Number: \n");
gets(a);

printf("\nEnter Second Number: \n");
gets(b);

printf("\nSum: \n");
}

void freeh(number **hd)
{
number *temp;

while(*hd!=NULL)
{
temp=*hd;
*hd=(*hd)->next;
free(temp);
}

*hd=NULL;
}

{
number *t;
t=(number*)malloc(sizeof(number));
t->next=NULL;
t->digit=i;

if(*hd==NULL)
{
*hd=t;
return;
}

t->next=*hd;
*hd=t;
}

void traverse(number *hd)
{
printf("\n");

while(hd!=NULL)
{
printf("%d",hd->digit);
hd=hd->next;
}
}

void reverse(number **hd)
{
number *p,*q,*r;

if(*hd==NULL)
return;

p=*hd;
if(p->next==NULL)
return;

q=p->next;
if(q->next==NULL)
{
q->next=p;
p->next=NULL;
*hd=q;
return;
}

r=q->next;
while(r->next!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}

q->next=p;
r->next=q;
(*hd)->next=NULL;
*hd=r;

}

{
int x,carry=0,sum=0;

reverse(&hd1);
reverse(&hd2);

while(hd1!=NULL&&hd2!=NULL)
{
x=(hd1)->digit+(hd2)->digit + carry;
sum=x % 10;
carry=x / 10;
hd1=(hd1)->next;
hd2=(hd2)->next;
}

if(hd1==NULL&&hd2!=NULL)
{
while(hd2!=NULL)
{
x=(hd2)->digit + carry;
sum=x % 10;
carry=x / 10;
hd2=(hd2)->next;
}
}
else if(hd1!=NULL&&hd2==NULL)
{
while(hd1!=NULL)
{
x=(hd1)->digit + carry;
sum=x % 10;
carry=x / 10;
hd1=(hd1)->next;
}
}

if(carry>0)

reverse(&hd1);
reverse(&hd2);
}

{
int i=0;

while(a[i]!= '\0')
{
i++;
}

reverse(hd);
}
``````