To handle numbers as large as 1064, we need to use a data structure that can store and manipulate such large values. In C++, the built-in integer types like int or long long are insufficient because they can only handle values up to 263−1 (for signed 64-bit integers). Instead, we can use strings to represent these large numbers and implement addition manually.
Here's a C++ program that adds two numbers represented as strings, each potentially up to 1064:
#include <iostream>
#include <string>
#include <algorithm>
// Function to add two large numbers represented as strings
std::string addLargeNumbers(const std::string& num1, const std::string& num2) {
// Result will store the sum of the two numbers
std::string result;
// Reverse both input strings to simplify addition from least significant digit
std::string s1 = num1;
std::string s2 = num2;
std::reverse(s1.begin(), s1.end());
std::reverse(s2.begin(), s2.end());
int carry = 0; // To store the carry during addition
int maxLength = std::max(s1.length(), s2.length());
for (int i = 0; i < maxLength || carry; ++i) {
// Get the current digit from each number (if available)
int digit1 = (i < s1.length()) ? (s1[i] - '0') : 0;
int digit2 = (i < s2.length()) ? (s2[i] - '0') : 0;
// Add the digits and the carry
int sum = digit1 + digit2 + carry;
carry = sum / 10; // Update carry
sum %= 10; // Store the single digit in the result
// Append the digit to the result string
result.push_back('0' + sum);
}
// Reverse the result back to its original order
std::reverse(result.begin(), result.end());
return result;
}
int main() {
// Input two large numbers as strings
std::string num1, num2;
std::cout << "Enter the first number: ";
std::cin >> num1;
std::cout << "Enter the second number: ";
std::cin >> num2;
// Ensure the inputs are valid non-negative integers
if (!std::all_of(num1.begin(), num1.end(), ::isdigit) ||
!std::all_of(num2.begin(), num2.end(), ::isdigit)) {
std::cerr << "Error: Inputs must be non-negative integers." << std::endl;
return 1;
}
// Add the two numbers
std::string result = addLargeNumbers(num1, num2);
// Output the result
std::cout << "The sum is: " << result << std::endl;
return 0;
}
Explanation of the Code:
-
String Representation:
- Large numbers are represented as strings since built-in numeric types cannot handle values this big.
-
Reversing Strings:
- The strings are reversed to make it easier to process digits starting from the least significant digit (rightmost).
-
Digit-by-Digit Addition:
- Each digit from both numbers is added along with any carry from the previous step.
- If one number has fewer digits than the other, missing digits are treated as
0.
-
Carry Handling:
- After adding two digits and the carry, the sum is split into a single digit (
sum % 10) and a carry (sum / 10).
-
Final Result:
- The result string is reversed again at the end to restore the correct order of digits.
-
Input Validation:
- The program checks that the inputs contain only digits to ensure they are valid non-negative integers.
Example Usage:
Input:
Enter the first number: 999999999999999999999999999999999999999999999999999999999999999
Enter the second number: 1
Output:
The sum is: 1000000000000000000000000000000000000000000000000000000000000000
Notes:
- This implementation assumes non-negative integers. If you need to handle negative numbers, additional logic would be required.
- The program handles very large numbers efficiently by processing them digit by digit.
Final Answer:
The provided code correctly adds two numbers up to 1064.