How to store a very large number of more than 100 digits in C++ Last Updated : 13 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an integer N in form of string str consisting of more than 100 digits, the task is to store the value for performing an arithmetic operation and print the given integer.Examples: Input: str = "54326789013892014531903492543267890138920145319034925432678901389201" Output: 54326789013892014531903492543267890138920145319034925432678901389201Input: str = "7890138920145319034925432678907890138920145319034925432678901903492543267890" Output: 7890138920145319034925432678907890138920145319034925432678901903492543267890 Approach: No data type is present in C++ to store 10100. So, the idea is to use get the input as string (as string can be of any length) and then convert this string into an array of digits of the length same as the length of string. Storing the big integer into an integer array will help to perform some basic arithmetic on that number. Below are the steps: Take the large number as input and store it in a string.Create an integer array arr[] of length same as the string size.Iterate over all characters (digits) of string str one by one and store that digits in the corresponding index of the array arr arr[i] = str[i] - '0';// Here '0' represents the digit 0, and // str[i] - '0' = ASCII(str[i]) - ASCII('0') = ASCII(str[i] - 48 Using the above step, we can store very large number for doing any arithmetic operations. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <iostream> using namespace std; // Function to return dynamic allocated // array consisting integers individually int* GetBigInteger(string str) { int x = str.size(), a = 0; // Create an array to store the big // integer into it. // Make the array size same as the // size of string str int* arr = new int[str.size()]; // Loop to extract string elements // into the array one by one while (a != x) { // Subtracting '0' to convert // each character into digit // str[a] - '0' // = ASCII(str[a]) - ASCII('0') // = ASCII(str[a] - 48 arr[a] = str[a] - '0'; a++; } // Return the reference of the array return arr; } // Driver Code int main() { // Big Integer in form of string str string str = "12345678098765431234567809876543"; // Function Call int* arr = GetBigInteger(str); // Print the digits in the arr[] for (int i = 0; i < str.size(); i++) { cout << arr[i]; } return 0; } Output: 12345678098765431234567809876543 Time Complexity: O(K), K is the number of digits in the number Auxiliary Space: O(K), K is the number of digits in the number Comment More infoAdvertise with us Next Article How to Read Multiple Numbers from a Single Line of Input in C++? S satyajitmahunta98 Follow Improve Article Tags : Misc Strings Mathematical C++ Programs DSA Arrays number-digits large-numbers cpp-input-output Numbers +6 More Practice Tags : ArraysMathematicalMiscNumbersStrings +1 More Similar Reads How to Handle Large Numbers in C++? In C++, when working with very large numbers the standard data types like int can become insufficient due to their limited range. In this article, we will learn how we can handle large numbers in C++. Handle Large Numbers in C++To store integers of different sizes, C++ offers built-in data types lik 3 min read How to Read Multiple Numbers from a Single Line of Input in C++? In C++, when working with user inputs we often need to take input of multiple numbers from a user in a single line. In this article, we will learn how to read multiple numbers from a single line of input in C++. Example Input: 1 7 0 4 6 8 Output: Entered Number: 1, 7, 0, 4, 6, 8Take Multiple Numbers 2 min read C++ Program to Find Factorial of a Large Number Using Recursion Given a large number N, task is to find the factorial of N using recursion. Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720. Examples: Input : N = 100Output : 93326215443944152681699238856266 2 min read C++ Program for Largest K digit number divisible by X Integers X and K are given. The task is to find highest K-digit number divisible by X. Examples: Input : X = 30, K = 3 Output : 990 990 is the largest three digit number divisible by 30. Input : X = 7, K = 2 Output : 98 An efficient solution is to use below formula. ans = MAX - (MAX % X) where MAX i 1 min read Square of large number represented as String Given a very large number, the task is to write a program to compute its square. Examples: Input: 9999 Output: 99980001 9999*9999 = 99980001 Input: 45454545 Output: 2066115661157025 45454545*45454545 = 2066115661157025 Naive Approach: A naive approach is to calculate the squares my multiplying the n 9 min read Data types that supports std::numeric_limits() in C++ The numeric_limits class template provides an easy and standardized way to query various properties of arithmetic types. For example, the maximum value a type T can store is std::numeric_limits<T>::max(). Example: std::numeric_limits<int>::max() gives the maximum possible value we can st 8 min read Like