C++ program to multiply large numbers has been shown here. This program follows the standard multiplication algorithm.
1. C++ program & output to Multiply Large numbers
Code has been copied
/************************************** alphabetacoder.com C++ program to multiply large numbers ***************************************/ #include <iostream> #include <cstring> //set maximum size of the input numbers #define MAX 500 using namespace std; int main() { // declare varables char num1[MAX], num2[MAX]; // for storing large numbers. int result[2 * MAX] = {0}; // for storing the result int n1, n2, d1, d2, i, j, sum, carry, flag = 0; // Read input numbers cout << "Enter the first number: "; cin >> num1; cout << "Enter the second number: "; cin >> num2; //find the number of digits for both numbers. n1 = strlen(num1); n2 = strlen(num2); // Perform multiplication // Multiply each digit of num1 with each digit of num2 for (i = n1 - 1; i >= 0; i--) { // initialize carry = 0; d1 = num1[i] - '0'; // current digit of first number for (j = n2 - 1; j >= 0; j--) { d2 = num2[j] - '0'; // current digit of second number // calculate sum sum = d1 * d2 + result[i + j + 1] + carry; result[i + j + 1] = sum % 10; // calculate carry carry = sum / 10; } // Set the carry to the leading digit result[i] = carry; } // display the result // discard the leading 0s in result array cout << "Result = "; for (i = 0; i < (n1 + n2); i++) { if (result[i]) flag = 1; if (flag) cout << result[i]; } return 0; }
Output
Enter the first number: 12456378945612399785623
Enter the second number: 2315628956456789323
Result = 28844351779058763001834553218068075303229