top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+1 vote
543 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 by Pavan P Naik

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+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.

The below program shows adding 64-bit numbers using Linked List

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

number *head3;

void main()
{
    number *head1=NULL,*head2=NULL;
    char a[500],b[500],c[500];
    char i;

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

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

    adds(a,&head1);
    adds(b,&head2);
    addhead(head1,head2);

    printf("\nSum: \n");
    traverse(head3);
    freeh(&head1);
    freeh(&head2);
}

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

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

    *hd=NULL;
}

void add(number **hd,int i)
{
    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;

}

void addhead(number *hd1,number *hd2)
{
    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;
        add(&head3,sum);
        hd1=(hd1)->next;
        hd2=(hd2)->next;
    }

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

    if(carry>0)
        add(&head3,carry);

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

void adds(char a[],number **hd)
{
    int i=0;

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

    reverse(hd);
}
answer Mar 18, 2014 by Sanjay Kumar
...