Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
1.3 kB
1
Indexable
Never
#include <iostream>
#include <vector>

using namespace std;

// Function to multiply two large integers represented as arrays
vector<int> multiply(vector<int>& num1, vector<int>& num2) {
    int m = num1.size();
    int n = num2.size();
    vector<int> result(m + n, 0);

    for (int i = m - 1; i >= 0; i--) {
        for (int j = n - 1; j >= 0; j--) {
            int mul = num1[i] * num2[j];
            int sum = result[i + j + 1] + mul;
            result[i + j + 1] = sum % 10;
            result[i + j] += sum / 10;
        }
    }

    // Remove leading zeros
    auto it = result.begin();
    while (it != result.end() && *it == 0) {
        it = result.erase(it);
    }

    return result;
}

int main() {
    string num1, num2;

    cout << "Enter the first large integer: ";
    cin >> num1;

    cout << "Enter the second large integer: ";
    cin >> num2;

    // Convert input strings to arrays of integers
    vector<int> array1(num1.size());
    vector<int> array2(num2.size());

    for (int i = 0; i < num1.size(); i++) {
        array1[i] = num1[i] - '0';
    }

    for (int i = 0; i < num2.size(); i++) {
        array2[i] = num2[i] - '0';
    }

    vector<int> result = multiply(array1, array2);

    cout << "Multiplication Result: ";
    for (int digit : result) {
        cout << digit;
    }

    return 0;
}
Leave a Comment