parsing a number

This is a discussion on parsing a number within the C Programming forums, part of the General Programming Boards category; to parse a number using modulus and division would you do this for example ( number = 12345 ) Code: ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    parsing a number

    to parse a number using modulus and division
    would you do this

    for example ( number = 12345 )

    Code:
    
    digit1 = ( number % 10 ) /1;      /*5*/
    digit2 = ( number % 100 ) / 10;    /*4*/
    digit3 = ( number % 1000 ) / 100;   /*3*/
    digit4 = ( number % 10000 ) / 1000;  /*2*/
    digit5 = ( number % 100000 ) / 10000; /*1*/
    the digits are integers so the decimals get truncated

    is there way another to parse the number using modulus and division

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    :-)
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int a = 12345;
    
      do
      {
        printf("%d\n", a % 10);
      }
      while (a /= 10);
    
      return 0;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 09:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 10:27 AM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 11:38 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

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