Multiplication of large numbers in c



1. Multiplication operation of very large numbers in c language
2.  How to get multiplication of two very large numbers larger or beyond than long int in c programming language

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000

char * multiply(char [],char[]);
int main(){
    char a[MAX];
    char b[MAX];
    char *c;
    int la,lb;
    int i;
    printf("Enter the first number : ");
    scanf("%s",a);
    printf("Enter the second number : ");
    scanf("%s",b);
    printf("Multiplication of two numbers : ");
    c = multiply(a,b);
    printf("%s",c);
    return 0;
}

char * multiply(char a[],char b[]){
    static char mul[MAX];
    char c[MAX];
    char temp[MAX];
    int la,lb;
    int i,j,k=0,x=0,y;
    long int r=0;
    long sum = 0;
    la=strlen(a)-1;
        lb=strlen(b)-1;
   
        for(i=0;i<=la;i++){
                a[i] = a[i] - 48;
        }

        for(i=0;i<=lb;i++){
                b[i] = b[i] - 48;
        }

    for(i=lb;i>=0;i--){
         r=0;
         for(j=la;j>=0;j--){
             temp[k++] = (b[i]*a[j] + r)%10;
             r = (b[i]*a[j]+r)/10;
         }
         temp[k++] = r;
         x++;
         for(y = 0;y<x;y++){
             temp[k++] = 0;
         }
    }
   
    k=0;
    r=0;
    for(i=0;i<la+lb+2;i++){
         sum =0;
         y=0;
         for(j=1;j<=lb+1;j++){
             if(i <= la+j){
                 sum = sum + temp[y+i];
             }
             y += j + la + 1;
         }
         c[k++] = (sum+r) %10;
         r = (sum+r)/10;
    }
    c[k] = r;
    j=0;
    for(i=k-1;i>=0;i--){
         mul[j++]=c[i] + 48;
    }
    mul[j]='\0';
    return mul;
}

Sample output of above code:
Enter the first number: 55555555
Enter the second number: 3333333333
Multiplication of two numbers:
185185183314814815

Logic for multiplication of large numbers

As we know in c there are not any such data types which can store a very large numbers.  For example we want to solve the expression:

55555555 * 3333333333


Result of above expression is very big number which beyond the range of even long int or long double.  Then question is how to store such a big numbers in c?  

Solution is very simple i.e. using array. Above program has used same logic that is we are using as usual logic to multiply two numbers except instead of storing the data in the normal variables we are storing into the array. 






19 comments:

Anonymous said...

Hi! Thanks for posting this code! Could you explain why you subtract 48 from each array element, and then do the %10 for temp, and /10 for r? What do temp and r represent?

Anonymous said...

good code , very interesting.
Hey @above this is just simple calculation as we do on paper.

thank you for the code

Anonymous said...

we subtract 48 to get the ascii for each digit. as we are using char array. And here r is remainder to carry over to the next element & temp[MAX] is a array for temporary calculations.

Anonymous said...

what does la+lb+2 signify?and for loops after that.?
Can u please clarify what happens after that!

Anonymous said...

why if(i <= la+j) and y += j + la + 1; is used?
Can u please clarify

ritesh kumar said...

ASCII value of character '0' is 48, '1' is 49 and so on. So if we will subtract 48 from it will become equivalent numeric value from character string. For example:
'0' - 48 = 0
'1' - 48 = 49-48 = 1
'9' - 48 = 57-48 = 9

In C % is reminder operator while / is division operator. For example:
10 % 3 = 1
10 / 3= 3

Anonymous said...

thnks for the code....
great code......

Anonymous said...

I wrote another program and if i do a[i] - 48....i'm getting weird characters of hearts and shapes. Not numbers. But i input abc...i get 1 2 3. Is it cuz of my input keyboard layout?

Anonymous said...

how will the code modify if use of pointers is avoided??

Anonymous said...

very difficult logic

Tarun Garg said...

yaar it is giving segmentation fault on spoj..:(

Anonymous said...

Won't work. What if carry results in two or even more digits. Say, if there are 1000 digits numbers and each have digit '9'. So in that case, resultant array will cause overflow causing segmentation fault due to carry generated.

Anonymous said...

48 is an exceedingly retarded way of writing '0' as it assumes an ASCII character set.

Mehboob Elahi said...

CODE....

#include
#include
#include
#define max 1000
int main()
{
char A[max],B[max];
int i,j,m,n,k,a,b,c,l;
int *r=(int *)calloc(max,sizeof (int));
printf("enter the two large number's \n'");
scanf("%s %s",&A,&B);

for(i=(strlen(B)-1);i>=0;--i)
{
int *p=(int *)calloc(max,sizeof (int));
for(j=(strlen(A)-1),k=0;j>=0;j--,k++)
{
b=(A[j]-48)*(B[i]-48);
b=*(p+k) +b;
*(p+k)=b%10;
*(p+k+1)=b/10;
}
if(*(p+k)==0)
--k;
for(m=(strlen(B)-1)-i,l=0;l<=k;l++,m++)
{
c=*(r+m)+*(p+l);
*(r+m)=c%10;
*(r+m+1)=*(r+m+1)+(c/10);
}

}
if(*(r+m)==0)
--m;
printf("the multiplication of the numbers is :");
for(i=m;i>=0;i--)
printf("%d",*(r+i));
return 0;
}

Mahfuz Billah said...

what about 10 or 11??

Unknown said...

can u plz explain the logic for y+=la+1...

Rajeshjoshi said...

10q

Shivam said...

There is a problem with the above code. When we multiply any two numbers of the form 10^a with 10^b (For example: 100 * 100 = 010000) then the product of the two numbers is preceded by an extra zero

karan suthar said...

#include
#include
int main()
{
int n=0,i,j,k,m=0,len,length,temp,toadd=0;
int *first,*second,*f=NULL;
char c[2];

first = (int *)malloc(sizeof(int)*100000);
second = (int *)malloc(sizeof(int)*100000);

printf("enter first number : ");
for(i=0;(c[0]=getchar())!='\n';i++,m++)
first[i] = atoi(c);

printf("enter second number : ");
for(i=0;(c[0]=getchar())!='\n';i++,n++)
second[i] = atoi(c);

first = (int *)realloc(first,sizeof(int)*(n));
second = (int *)realloc(second,sizeof(int)*(m));

length = len = n+m;
f = ( int *)malloc(len*sizeof(int));
for(i=0;i=0;i--,k=--len-1)
for(j=n-1;j>=0;j--)
{
temp = first[j]*second[i];
f[k] += temp % 10;
f[k-1] += f[k]/10 + temp/10;
f[k]%=10;
k--;
}

printf("product is :\n");
if(*(f)==0)
i=1;
else
i=0;
for(i=i;i<length;i++)
printf("%d",f[i]);

free(f);
free(first);
free(second);
return 0;
}