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 corresponsing 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 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; } |
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find smallest possible Number from a given large Number with same count of digits
- Given a large number, check if a subsequence of digits is divisible by 8
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number of factors of very large number N modulo M where M is any prime number
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Minimum number of digits to be removed so that no two consecutive digits are same
- Check if a large number is divisible by a number which is a power of 2
- Check if the sum of digits of number is divisible by all of its digits
- Smallest number with given sum of digits and sum of square of digits
- Divisibility by 12 for a large number
- Factorial of a large number
- Sum of the digits of square of the given number which has only 1's as its digits
- Multiply large integers under large modulo
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

