MY name is ruhul amin

ISLAMIC UNIVERSITY OF KUSHTIA

Saturday, March 9, 2013

big integer multiplication

#include<stdio.h>
#include<string.h>
int main()
{
    int i,j,k,l,la,lb,r,h,x,y,s,t,n,z,w;
    char a[140120],b[140120],c[140120],d[140120],v[140120];
    while(scanf("%s%s",c,d)==2)
    {
        la=strlen(c);
        lb=strlen(d);
        l=la+lb;
        j=0;
        for(i=la-1;i>=0;i--)
        {
            a[j]=c[i];
            j++;
        }
        k=0;
        for(t=lb-1;t>=0;t--)
        {
            b[k]=d[t];
            k++;
        }
        for(n=0;n<l;n++)
        v[n]=48;
        v[n]='\0';
        r=-1;
        for(x=0;x<lb;x++)
        {
            h=0;
            for(y=0;y<la;y++)
            {
                s=(a[y]-48)*(b[x]-48)+(v[x+y]-48)+h;
                v[x+y]=s%10+48;
                h=s/10;
                if((x+y)>r)
                r=x+y;
            }
        while(h!=0)
        {
            s=h+(v[x+y]-48);
            v[x+y]=s%10+48;
            h=s/10;
            if(x+y>r)
            r=x+y;
            y++;
        }
        }
        for(z=r;z>0;z--)
        {
            if(v[z]==48)
            r--;
            if(v[z]>48)
            break;
        }
        for(w=r;w>=0;w--)
        printf("%c",v[w]);
        printf("\n");

    }
    return 0;
}

No comments:

Post a Comment