I have a number, 321197186 which the user inputs. How can I store this number into an array one element at a time. Basically, I am trying to store the 1st digit into 0th element and so on. And then I have to do some computation on that number.
-
1just manage that number as a string when you read it, and that string is your array, after for each char remove `'0'` and you got the digit value to do your computations. An other way is to read char per char etc – bruno Oct 03 '20 at 18:37
-
but that wouldn't work right because I cant read the string, I can only read one character at a time. – Gaz96 Oct 03 '20 at 18:41
-
1so read char par char and remove `'0'` for each to get the digit value, as I said in previous remark – bruno Oct 03 '20 at 18:42
5 Answers
Represent your number as a string, where every character of that string will be the corresponding digit of your number.
There are a plethora of method to read a string, but I suggest you use fgets() like this:
#include <stdio.h>
#include <string.h>
#define MAX_LEN 10
int main (int argc, char *argv[])
{
char number[MAX_LEN];
printf("Enter a number: \n");
fgets(number, MAX_LEN , stdin);
printf("%s\n", number);
return 0;
}
With a for loop, you access the characters (digits) of the string (number) one by one, if you like.
I suggest you also eat the trailing newline that fgets()
leaves in the input array, as already explained in Removing trailing newline character from fgets() input.
Alternative solution proposed by bruno@:
scanf("%9s", number);
Read more in C - scanf() vs gets() vs fgets().

- 71,951
- 46
- 188
- 305
-
why so complicated when `scanf("%9s", number);`does the work and does not save the possible spaces (in *isspace* definition) before and after ? – bruno Oct 03 '20 at 19:40
-
@bruno `scanf()` is more prune to error than `fgets()`, but it's a fair point, I'll add it as an alternative solution, thanks! – gsamaras Oct 03 '20 at 22:13
Assume you have
int num;
if (scanf("%d", &num) != 1)
{
// Input error
exit(1);
}
// Now the user input is available in num
then you do
#define MAX_DIGITS 32
int digits[MAX_DIGITS] = { 0 };
int index = 0;
while(num != 0 && index < MAX_DIGITS)
{
digits[index] = num % 10;
++index;
num = num / 10;
}
// Now the array digits holds the individual digits
// and index holds the number of valid digits

- 42,271
- 4
- 38
- 63
-
`do {...} (num != 0 && index < MAX_DIGITS);` handle also nicely handles `num == 0`. – chux - Reinstate Monica Oct 03 '20 at 19:11
-
`#define MAX_DIGITS 32` is very large considering you decided to read an *int*, and you do not need to make that kind of assumption because the number of digit of the max int can be known at compile time in several ways – bruno Oct 03 '20 at 19:21
-
You can consider this logic in C -
number = 321197186
while(number> 0) - leave if number becomes 0
{
int last_digit = number % 10; - Last digit from the number
printf("%d",last_digit);
number = number / 10; - to get the remaining number.
}
This can be also done in Python as -
number = 321197186
list1 = []
for i in str(number):
list1.append(i)
print(list1)

- 113
- 5
-
not sure that helps a lot the OP, you do not put the digits in an array as required, and Python proposal is not requested too – bruno Oct 03 '20 at 19:23
-
Thanks for your comment bruno, I think logic is the key and putting the output in an array can be taken care of. – kartik sharma Oct 03 '20 at 19:39
store this number into an array one element at a time.
Let us take advantage of the input process to have a convenient way to determine the length of input including leading zeros (but not input with a sign, leading spaces) by using "%n"
to record the offset of the scan.
Use % 10
to exact the least significant decimal digit of the number.
int number;
int offset;
if (scanf("%d%n", &number, &offset) == 1) {
int a[offset]; // VLA
while (offset > 0) {
a[--offset] = number % 10;
number /= 10;
}
for (int i = 0; i < sizeof a/sizeof a[0]; i++) {
printf("a[%d] = %d\n", i, a[i]);
}
}
Output
a[0] = 3
a[1] = 2
a[2] = 1
a[3] = 1
a[4] = 9
a[5] = 7
a[6] = 1
a[7] = 8
a[8] = 6

- 143,097
- 13
- 135
- 256
But as bruno points out, you just have to check it as a string as an user input with fgets is an array of characters (even if we are talking about numbers). For more detail:
for(i=0;i<sizeof(array_input);i++){
//Store the char into another array
}

- 136
- 1
- 9