Uva 10106 Working

#include<bits/stdc++.h>
using namespace std;
string s[100];
string sum = "";
void multiple(string a, string b)
{
    int l1 = a.size();
    int l2 = b.size();
    int c, d, r, n, m, i, j, k, g;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    for(i=0; i<l2; i++)
    {
        c = b[i] - '0';
        for(k=1; k<=i; k++)
            s[i] = s[i] + "0";
        r = 0;
        for(j=0; j<l1; j++)
        {
            d = a[j] - '0';
            n = (c*d) + r;
            if(j == l1-1)
            {
                if(n == 0)
                {
                    s[i] = s[i] + "0";
                }
                else
                {
                    while(n != 0)
                    {
                        g = n%10;
                        string p = to_string(g);
                        s[i] = s[i] + p;
                        n = n/10;
                    }
                    r = 0;
                }
            }
            else
            {
                if(n>9)
                {
                    m = n%10;
                    r = n/10;
                }
                else
                {
                    m = n;
                    r = 0;
                }
                string p = to_string(m);
                s[i] = s[i] + p;
            }
            n = d = 0;
        }
    }
}
string summation(string a, string b)
{
    string x = "";
    int p;
    int l1 = a.size();
    int l2 = b.size();
    if(l1 > l2 || l1 == l2)
        p = l1;
    else
        p = l2;
    int i, j, n, m, c, d, r, k;
    i = j = n = m = c = d = r = k = 0;
    while(i<l1 || j<l2)
    {
        if(i<l1)
        {
            n = a[i++] - '0';
        }
        if(j<l2)
        {
            m = b[j++] - '0';
        }
        c = n+m+r;
        r = 0;
        if(k == p-1)
        {
            if(c == 0)
            {
                x = x + "0";
            }
            else
            {
                while(c != 0)
                {
                    int g = c%10;
                    x = x + to_string(g);
                    c = c/10;
                }
            }
        }
        else
        {
            if(c > 9)
            {
                r = c/10;
                c = c % 10;
            }
            x = x + to_string(c);
            n = m = c = 0;
        }
        k++;
    }
    return x;
}
int main()
{
    string a, b;
    cin >> a >> b;
    int p, i;
    int l1 = a.size();
    int l2 = b.size();
    if(l1 > l2 || l1 == l2)
    {
        multiple(a, b);
        p = l2;
    }
    else
    {
        multiple(b, a);
        p = l1;
    }
    for(i=0; i<p; i++)
    {
        sum = summation(sum, s[i]);
    }
    reverse(sum.begin(), sum.end());
    int f = 1;
    for(i=0;i<sum.size();i++)
    {
        if(sum[i] != '0')
        {
            f = 0;
            break;
        }
    }
    if(f == 1)
        cout << 0 << endl;
    else
        cout << sum << endl;
    return 0;
}

Comments