I'm trying to implement Karatsuba multiplication through recursive calls. The code below should work, but I keep getting the Zero as answer. Any thoughts?
#define ll long long int
ll kmul(ll p, ll q)
{
ll a,b,c,d,ans;
ll n=0;
while(p)
{
p=p/10;
n++;
}
//cout<<n<<endl;
if(n<2)
{
return p*q;
}
else
{
int k=n/2;
ll j=pow(10,k);
a=p/j;
b=p%j;
c=q/(j);
b=q%j;
ans=(pow(10,n)*(kmul(a,c)))+(j*kmul(a,d)*kmul(b,c))+kmul(b,d);
return ans;
}
}