Multiply Strings

Multiple two strings for an example:

string s1 = "123"; string s2 = "45";

Result= 5535

public class Program
{
    

    public int MultiplyStrings(string s1, string s2)
    {
        int m = s1.Length;
        int n = s2.Length;

        // Initialize result variable to store the final multiplication result
        int finalResult = 0;

        // Multiply each digit and accumulate results
        for (int i = m - 1; i >= 0; i--)
        {
            for (int j = n - 1; j >= 0; j--)
            {
                int digit1 = s1[i] - '0';
                int digit2 = s2[j] - '0';

                // Calculate the position to add the product
                int position = (m - 1 - i) + (n - 1 - j);

                // Multiply and accumulate into finalResult
                finalResult += digit1 * digit2 * (int)Math.Pow(10, position);
            }
        }

        return finalResult;
    }
}

Last updated