top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to multiply two numbers represented by array of integers?

+2 votes
1,131 views
How to multiply two numbers represented by array of integers?
posted Mar 2, 2016 by anonymous

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

3 Answers

0 votes

multiply two numbers represented by array of integers

#include <iostream>

void multipy(char* a, char* b, char* res)
{
    int lenA = strlen(a);
    int lenB = strlen(b);

    int i, j;


    int* c = new int[lenA + lenB];
    memset(c, 0, sizeof(int) * (strlen(a) + strlen(b)));

    for (i = lenA - 1; i >= 0; i--)
        for (j = lenB - 1; j >= 0; j--)                 
            c[i + j + 1] += (b[j] - '0') * (a[i] - '0');

    for (i = lenA + lenB - 1; i >= 0; i--)
    {
        if (c[i] >= 10)
        {
            c[i - 1] += c[i] / 10;
            c[i] %= 10;
        }
    }

    i = 0;
    while (c[i] == 0)
        i++;

    j = 0;
    while (i < lenA + lenB)
    {
        res[j] = c[i] + '0';
        i++; j++;
    }

    res[j] = 0;
    delete[] c;
};

int main()
{
    char* a = "999";
    char* b = "9999";
    char* res = new char[strlen(a) + strlen(b)];

    multipy(a, b, res);
    return 0;
}
answer Mar 4, 2016 by Manikandan J
0 votes

Check the following Program -

#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: **********
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 * **********

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.

answer Mar 8, 2016 by Ajay Kumar Topno
0 votes

include<stdio.h>

include<conio.h>

include<math.h>

int convert(int *p,int size)
{
int i=0;
int j=size-1;
int sum = 0;
int temp;

while(i != size)
{
temp = pow(10,i);
sum =sum + (j[p] * temp);
j--;
i++;
}
return sum;
}

int main(void)
{
int br_1[] = {1,2,3,4,5};
int br_2[] = {5,4,3,2,1};
int br1,br2;

br1 = convert(br_1,(sizeof(br_1)/sizeof(int))); /* (sizeof(br_1)/sizeof(int)) number of elements in array */
br2 = convert(br_2,(sizeof(br_1)/sizeof(int)));

printf("-------%d-------",br1+br2);

getchar();
getchar();
return 0;}

answer Apr 17, 2017 by Leon Martinović
Similar Questions
+2 votes

Provide an algorithm to add and multiply two large integers which cannot be represented by built-in types, C code would be a great help?

+7 votes

Input:
First List: 5->6->3 // represents number 563
Second List: 8->4->2 // represents number 842
Output
Resultant list: 4->7->4->0->4->6 // represents number 474046

0 votes

You are given 2 long integers having n digits each and you are required to multiply them using C.

Assumptions
Numbers are represented in an array of size n .
Calculate the time complexity using traditional divide and conquer

+4 votes

Say I have given two big numbers i.e. 10^8 and 10^9 what could be the best way to multiply them without facing the overflow issue.

...