Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,123 questions

55,997 answers

573 users

How to multiply two long numbers in C#

1 Answer

0 votes
/* Multiply Two Long Numbers
   ------------------------------------
   Implements manual big‑integer multiplication
   using decimal strings.
*/

using System;
using System.Text;

public sealed class LongNumber
{
    public string Value { get; }

    public LongNumber(string v) {
        Value = Normalize(v);
    }

    /* Multiply two integer strings a * b and return the result as a string.
       Caller receives a normal C# string.
       No aliasing issues because strings are immutable. */
    public LongNumber Multiply(LongNumber other)
    {
        string a = Value;
        string b = other.Value;

        // Handle sign
        bool sign = false;
        if (a.StartsWith("-")) {
            sign = !sign;
            a = a[1..];
        }
        if (b.StartsWith("-")) {
            sign = !sign;
            b = b[1..];
        }

        // If either is "0", return "0"
        if (a == "0" || b == "0")
            return new LongNumber("0");

        int la = a.Length;
        int lb = b.Length;
        int[] digits = new int[la + lb];

        /* Multiply from right to left */
        for (int i = la - 1; i >= 0; i--) {
            char ca = a[i];
            if (!char.IsDigit(ca)) continue;

            for (int j = lb - 1; j >= 0; j--) {
                char cb = b[j];
                if (!char.IsDigit(cb)) continue;

                int pos = i + j + 1;
                int sum = digits[pos] + (ca - '0') * (cb - '0');

                digits[pos] = sum % 10;
                digits[pos - 1] += sum / 10;
            }
        }

        // Convert digits[] to string, skipping leading zero
        var sb = new StringBuilder();
        int start = digits[0] == 0 ? 1 : 0;

        for (int i = start; i < digits.Length; i++)
            sb.Append((char)('0' + digits[i]));

        // Add sign if needed
        if (sign)
            sb.Insert(0, '-');

        return new LongNumber(sb.ToString());
    }

    private static string Normalize(string s)
    {
        // Skip leading spaces
        s = s.TrimStart();

        // Remove leading zeros (but leave one zero)
        int i = 0;
        while (i < s.Length - 1 && s[i] == '0')
            i++;

        return s[i..];
    }

    public override string ToString() => Value;
}

public static class Program
{
    public static void Main()
    {
        var a = new LongNumber(" 18361891827367132321");
        var b = new LongNumber("-18361891827367132321");

        var result = a.Multiply(b);
        Console.WriteLine(result);
    }
}



/*
run:

-337159071479931885857929667075122847041

*/

 



answered Jun 18 by avibootz
...