Is there a way to separate whole int to a single digit???

This is a discussion on Is there a way to separate whole int to a single digit??? within the C Programming forums, part of the General Programming Boards category; for example i got int of 15569542 and i wan to separate them to single digit like 1 5 5 ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    8

    Is there a way to separate whole int to a single digit???

    for example i got int of 15569542 and i wan to separate them to single digit like
    1
    5
    5
    6
    9
    5
    4
    2
    please help and thank for ur help

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Compute int%10 (modulo 10). Store the result (last digit) in some array.
    2) Divide int by 10.
    3) Repeat

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    int asd=98765, k[5], i;
    for(i=0;i<5;i++)
    {
    k[i]=asd%10;
    asd=asd/10;
    }
    like this correct??
    Last edited by lancetky; 04-17-2010 at 12:08 PM.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well yes, but that assumes that you know how many digits there are in advance. A more general way would be:

    Code:
    int asd = 98321, k[5], i =0;
    while(asd > 0){
      k[i++] = asd % 10;
      asd = asd/10;
    }
    
    /* To print the asd digits in the right order you must print k[] in reverse */
    int j;
    for(j = i-1;j >= 0;j--){
     printf("%d",k[j]);
    }
    I haven't tested this but it's something along these lines.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    8
    i think i got it. thank for ur help =D

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You're welcome.

  7. #7
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Here's an alternate way ....

    Code:
    #include <stdio.h>
    
    #define MAX             999999
    #define str(x)          # x
    #define xstr(x)         str(x)
    
    int main(void)
    {
      int d;
      char array[sizeof xstr(MAX)];
    
      fputs("Enter an integer from 0 to " xstr(MAX) ": ", stdout);
      fflush(stdout);
      if (fscanf(stdin, "%d", &d) == 1 && d >= 0 && MAX >= d) {
        sprintf(array, "%d", d);
        for (d = 0; array[d] != '\0'; ++d) {
          printf("%c\n", array[d]);
        }
      } else {
        puts("Try again.");
      }
      return 0;
    
    }
    $ gcc -Wall -Wextra alt.c -o alt
    [cd@localhost oakland]$ ./alt
    Enter an integer from 0 to 999999: 923451
    9
    2
    3
    4
    5
    1
    [cd@localhost oakland]$

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21