Multiply two large numbers..


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;

char *multiply_large_num( const char* first, const char* second ){
    int len_f = strlen( first );
    int len_s = strlen( second );
    char* output = new char[ len_f + len_s];
    memset( output, 0, sizeof(char) * ( len_f + len_s ));

    for( int i = len_f - 1; i >= 0; i-- )
    {
        for( int j = len_s - 1; j >= 0; j-- )
        {
            output[ i + j + 1 ] += ( first[ i ] - '0' ) * ( second[ j ] - '0');
            output[ i + j ] += output[ i + j + 1 ]/10;
            output[ i + j + 1 ] = output[ i + j + 1 ]%10;
        }
    }
    for(int i=0;i<len_f+len_s;i++)
        printf("%d",output[i]);

}

int main( void ){
    char* first=new char[1000];
    char* second=new char[1000];
    cin>>first;
    cin>>second;
    multiply_large_num( first, second );
    return 0;
}

Comments

Popular Posts