Integer Multiplication

C++ Code

/* Program for integer multiplication */
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
//function to calculate size of a given number in form of array
int size(char a[])
{
    int length=0;
    //loop till end of the array
    while(a[length]!='\0')
    {
        length++;
    }
    return length;
}
//funtion to add bits of number
void add(char x[],char y[])
{
    int sx,sy,flag=0;
    sx=size(x);
    sy=size(y);
    for(int i=0;i<sx;i++)
    {
        y[i]=char((int)x[i]-48+(int)y[i]);
    }
}
//function for integer multiplication
long int multiply(char x[],char y[])
{
    long int n,p1,p2,p3;
    n=max(size(x),size(y));
    char xl[(n/2)+1],xr[(n/2)+1],yl[(n/2)+1],yr[(n/2)+1],tmpx[(n/2)+1],tmpy[(n/2)+1];
    if(n==1)
    {
        return ((int)x[0]-48)*((int)y[0]-48);
    }
    //divide x into 2 parts
    strcpy(xl,x);
    xl[n/2]='\0';
    strcpy(xr,x+(n/2));
    xr[n]='\0';
    //divide y into 2 parts
    strcpy(yl,y);
    yl[n/2]='\0';
    strcpy(yr,y+(n/2));
    yr[n]='\0';
    //multiply higher order bits of x and y
    p1=multiply(xl,yl);
    //multiply lower order bits of x and y
    p2=multiply(xr,yr);
    strcpy(tmpx,xr);
    strcpy(tmpy,yr);
    add(xl,tmpx);
    add(yl,tmpy);
    //multiply xl+yl with xr+yr
    p3=multiply(tmpx,tmpy);
    //return result
    return p1*pow(2,n)+(p3-p1-p2)*pow(2,n/2)+p2;
}
int main()
{
    char x[100],y[100];
    cin>>x;
    cin>>y;
    cout<<multiply(x,y);
}


JAVA Code

/*Integer Multiplication Problem*/
import java.io.*;
class integermult
{  
	 long Second;
	 long First; 
     long half(long a,long loc,long f)
	 {
        if(loc==0)
	   	return(a%f);
	    else
	    return(a/f);
	 }

	 long size(long a)
	 {
        long f=0,k;
       	k=a;
	    while(k!=0)
	    {
			f++;
			k=k/10;
	    }
	    if(f%2==1)
	    f=f+1;
	    return(f);
	  }

	 long factor(long size)
	 {
        long i,f=1;
	    for(i=1;i<=size/2;i++)
	    f=f*10;
	    return(f);
	 }
	 long max(long a,long b)
	 {
        if(a>b)
		return(a);
		else
		return(b);
	 }

	 long multiply(long a,long b)
	 {
        long s,f;
	    s=size(max(a,b));
	    f=factor(s);
	    long s1,s2,s3,ans;
	    if(s>2)
	    {
			 s1=multiply(half(a,1,f),half(b,1,f));
			 s2=multiply((half(a,1,f)-half(a,0,f)),(half(b,0,f)-half(b,1,f)));
			 s3=multiply(half(a,0,f),half(b,0,f));
        }
	    else
	    {
            s1=half(a,1,f)*half(b,1,f);
			s2=(half(a,1,f)-half(a,0,f))*(half(b,0,f)-half(b,1,f));
			s3=half(a,0,f)*half(b,0,f);
        }
	    ans=(s1*factor(s)*factor(s))+((s1+s2+s3)*factor(s))+s3;
	    return(ans);
	 }
        public static void main(String args[]) throws IOException
        {
            integermult m= new integermult();
            long a,b;
            System.out.println("\nenter the value of a in binary\n");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            m.Second  = Integer.parseInt(br.readLine());
            System.out.println("\nenter the value of b in binary\n");
            m.First=Integer.parseInt(br.readLine());       
            System.out.println("The product is: "+m.multiply(m.Second,m.First));
        }
}