Input an integer array without spaces in C
How to input a large number (a number that cannot be stored even in long long int) without spaces? We need this large number in an integer array such that every array element stores a single digit.
Input : 10000000000000000000000000000000000000000000000
We need to read it in an arr[] = {1, 0, 0...., 0}
In C scanf(), we can specify count of digits to read at a time. Here we need to simply read one digit at a time, so we use %1d.
// C program to read a large number digit by digit #include <stdio.h> int main() { // array to store digits int a[100000]; int i, number_of_digits; scanf("%d", &number;_of_digits); for (i = 0; i < number_of_digits; i++) { // %1d reads a single digit scanf("%1d", &a;[i]); } for (i = 0; i < number_of_digits; i++) printf("%d ", a[i]); return 0; } |
Input :
27 123456789123456789123456789
Output :
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
We can also use above style to read any number of fixed digits. For example, below code reads a large number in groups of 3 digits.
// C program to read a large number digit by digit #include <stdio.h> int main() { // array to store digits int a[100000]; int i, count; scanf("%d", &count;); for (i = 0; i < count; i++) { // %1d reads a single digit scanf("%3d", &a;[i]); } for (i = 0; i < count; i++) printf("%d ", a[i]); return 0; } |
Input :
9 123456789123456789123456789
Output :
123 456 789 123 456 789 123 456 789
Same concept can be used when we read from a file using fscanf()
Recommended Posts:
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Declare a C/C++ function returning pointer to array of integer pointers
- Removing spaces from a string using Stringstream
- Lex program to count the number of lines, spaces and tabs
- Integer Promotions in C
- How to Read and Print an Integer value in C
- How to Read and Print an Integer value in C++
- Integer literal in C/C++ (Prefixes and Suffixes)
- Check for integer overflow on multiplication
- How to concatenate two integer arrays without using loop in C ?
- Assigning an integer to float and comparison in C/C++
- Storage of integer and character values in C
- A creative C++ program to Zoom digits of an integer
- Extended Integral Types (Choosing the correct integer size in C/C++)
- Input Iterators in C++
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.



