# Thread: Finding the first digit in a number....help

1. ## Finding the first digit in a number....help

Hey guys, just wanted to first off say thanks for all the help in the past.

I was wondering how to go about finding the first digit from a number inputted into an array.

Thank you for any help.

ex.
enter the numbers to use:
9
45
682
120

output:
the numbers are 9 4 6 and 1

Here is what I have, the array goes to a max of 15 for the moment, but I allow the user to input as large an array as they want up to that number. Then the user is to input it's numbers.
Code:
```#include <stdio.h>

#define MAX 15
int main()
{

int array[MAX]; /*Array to place numbers in*/
int num;        /*Amount of number to use in game*/
int num1;       /*Number to be inputted for game*/
int i, j, k;

printf("How many numbers would you like to have?\n");
scanf("%d", &num);

for(i = 0; i < num ; i++){  /*Starts loop to input numbers into array*/
scanf("%d", &num1);
array[i] = num1;
}

printf("\n");

for(k = 0; k < num; k++){  /*Loop prints the array*/
printf("%d ", array[k]);
}

return 0;
}```

2. for the moment i have found it on this thread.
Code:
``` while (num1 >= 10)
num1 /= 10;```

3. > Such as turning it to a string?
How do you think the code turns a number into a decimal string to begin with?

Yes, a loop consisting of /10 and %10.
And some extra baggage as well.

Add a check for negative numbers, and you're golden.

4. Such as turning it to a string?
Yes, definitely... well, actually, forget about turning it into an integer. If all you're going to do is find the first digit, you might as well this:
Code:
```char mynumber[20];

/*in loop*/
scanf("%s", &mynumber);
array[i] = (int) mynumber[0] - (int) '0');```
Obviously this is incomplete but the idea is there; also you'll want to protect against the evils of the scanf("%s") security hole, and look for the first actual digit [skipping '-' if it's there] in the string. "I'll leave that to the reader as an exercise."