0

How can I read a digit as single digit within a number?

int main() {
long long int num1;
printf("enter number: ");
scanf("%lld", &num1);

for example if user input is 123456789, how can I use 4 as a single digit?

Batool
  • 81
  • 1
  • 1
  • 9
  • 1
    There's really no such thing as a "digit" in a "number", For example, decimal "123" == octal "723" - is the first digit "1" or "7"? Perhaps you mean a character string, like "0x31323300" (for the ASII characters "1", "2" and "3")? – paulsm4 Dec 06 '15 at 02:14

4 Answers4

4

Because the scanf() call in the question uses the %lld specifier, which interprets input in base ten, we can assume you want digits in base ten. Assuming the number is positive:

  1. First divide by a power of ten. To extract the sixth digit from the right, divide by a power of 10 that has six digits, such as 100000. This gives 1234. If you want to retrieve a variable digit, you can use repeated division by 10 to move the correct digit into the one's place, as shown in Ashiquzzaman's answer. If you instead want digits from the left, start by counting digits in the number: divide it by 10 until the quotient is zero.
  2. Use the modulo operator % to extract the remainder from division by 10. This gives 4.

If the number is negative, division in C rounds toward zero. This means -123456789 will produce -1234 after the first step and -4 after the second.

Community
  • 1
  • 1
Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64
  • @paulsm4 it's not a silly question. I am new to C, so give me a break. =) – Batool Dec 06 '15 at 03:16
  • OK, Batool. But do you get that there's really no absolute meaning as to a "digit" in a "number"? Do you get that *all* of the responses you got (as well as several other, different responses you might have gotten) are "correct" .... depending on how you define the problem? As a character in a string, as a result in a base-ten arithmetic expression ... or perhaps as "something else" entirely? It really has nothing to do with "C" ... and everything to do with "understanding computing". – paulsm4 Dec 06 '15 at 06:46
  • @paulsm4 There's an absolute meaning when the question's `scanf()` call uses an explicitly decimal specifier `"%lld"`. – Damian Yerrick Dec 07 '15 at 18:59
1

You can take input as long long int then extract the digit as follow:

#include <stdio.h>
#include<math.h>
int getDigit(long long number, int idx)
{
    number = abs(number);
    while(idx--)
        number/=10;
    return number%10;
}
int main()
{
    long long int num1;
    int indexOfDigit = 4;
    printf("enter number: ");
    scanf("%lld", &num1);
    printf("%d\n", getDigit(num1, indexOfDigit));
}

Sample input: 123456789

Sample Output: 5

For Explanation see the answer of tepples

Community
  • 1
  • 1
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
0

Read it as a sequence of chars (basically, as if it were a string), using %c in scanf in a loop until EOF is read

int main()
{
  int digit;

  printf("enter number: ");
  digit = getchar();
  while (digit != EOF)
  {
    /* Process digit */
    digit = getchar();
  }
}
mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
0

You can store the input as a char* and access to the digits with char[3] for the fourth digit?

ndrrr
  • 36
  • 4