Tutorial on how to implement the multiplication of large numbers in C language and the multiplication of large numbers
This algorithm has the following tips: convert a string into an array, use the multiplication formula c [I + j] + = B [I] * a [j], and then process carry in a unified manner, the essence of this step
#include<stdio.h>
#include<string.h>
#define MAX 200
Void Muti(char str1[],char str2[])
{
Int len1,len2,i,j;
Int a[MAX+10],b[MAX+10],c[2*MAX+20];
Memset(a,0,sizeof(a)); //Array initialization
Memset(b,0,sizeof(b));
Memset(c,0,sizeof(c));
Len1 = strlen(str1);
For(j=0,i=len1-1;i>=0;i--)
{
a[j++] = str1[i] - '0' ;
}
Len2 = strlen(str2);
For(j=0,i=len2-1;i>=0;i--)
{
b[j++] = str2[i] - '0';
}
For(i=0;i<len2;i++)
{
For(j=0;j<len1;j++)
{
c[i+j] += b[i]*a[j];
}
}
For(i=0;i<MAX*2;i++) //Circular unified processing carry problem
If(c[i]>=10)
{
c[i+1]+=c[i]/10;
c[i]%=10;
}
For(i=2*MAX;(c[i]==0)&&(i>=0);i--);//skip the highest bit of 0
If(i>=0)
For(;i>=0;i--)
Printf("%d",c[i]);
Else
Printf("0");
Printf("\n");
}
Int main()
{
Char str1[MAX],str2[MAX];
Gets(str1);
Gets(str2);
Muti(str1,str2);
}