Thread: Separates digits

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    10

    Exclamation Separates digits

    Hi all

    anyone can give me any idea that how to do this using on single selection of if statment
    "Write a program that inputs one five-digit number,separates the number into its individual digits and prints the digit seperately from one another.
    like if input is 42139
    the output

    4 2 1 3 9"

    any idea??

    Best Regards
    Asad

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    here is a "hint"
    Use the "%" operator.
    for example 98 % 10 gives u 8

    that will help u separate the digits. put the whole thing in a loop, let the loop run as many times as the number of digits in ur original number.
    at the end of the loop, U have all the digits.
    In the middle of difficulty, lies opportunity

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    failed....

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Um, so I guess the problem is on line 42?
    http://cboard.cprogramming.com/annou...t.php?f=4&a=39
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    how shud i separate the digit and with which digit i shud use for "%"

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Post your attempt!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int a = 98;
    int b = 10;
    int rem;
    int c;
    clrscr();
    rem = a % b;
    c = rem + b;
    printf(" : %d %d", c, rem);
    
    
    
    return 0;
    
    }
    it prints only for 9 n 8

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Asad_Rehman
    it prints only for 9 n 8
    : 18 8
    Division is the other part of remainder after division. It is done with the / operator.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    hmm thankx let me try
    edit: where should i do i m completely feeling confused:S
    Last edited by Asad_Rehman; 10-28-2006 at 11:48 PM.

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Here's an example with 2 numbers :

    19 = 1*10^1 + 9*10^0

    19 % 10 = 9 --> Store this as the first digit (or last to print out)

    floor(19/10) = 1 --> This is the result you'll now have to compare for your next digit

    1 % 10 = 1 --> Store this as the second digit (or first to print out)

    print out 1
    print out 9.

    There you go. Now do it for five numbers.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Extra large hint (I'm off to bed):
    Code:
    #include<stdio.h>
    
    int main()
    {
       int a = 123;
       int div = a / 10;
       int rem = a % 10;
       printf("div = %d, rem = %d\n", div, rem);
    
       a = div;
       div = a / 10;
       rem = a % 10;
       printf("div = %d, rem = %d\n", div, rem);
       return 0;
    }
    
    /* my output
    div = 12, rem = 3
    div = 1, rem = 2
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    thanks

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int a;
    int b = 10;
    int rem,c;
    clrscr();
    
    printf("\n Enter a five digit number: ");
    scanf("%d", &a);
    
    rem = a / b;
    c = a % b ;
    printf("\n : %d", c);
    a = rem;
    rem = a / b;
    c = a % b ;
    printf(" %d", c);
    a = rem;
    rem = a / b;
    c = a % b ;
    printf(" %d", c);
    a = rem;
    rem = a / b;
    c = a % b ;
    printf(" %d", c);
    a = rem;
    rem = a / b;
    c = a % b ;
    printf(" %d", c);
    
    return 0;
    
    }

    for 42139 it gives a negative value and for 11111 it prints 1 1 1 1 1
    edit: Sorry mates just read the integer types was just confused fully sorrryy once again
    Last edited by Asad_Rehman; 10-29-2006 at 12:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 04:05 PM
  5. Odd/Even Digits in a Number-Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 10:39 PM