Thread: parsing a number

  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, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04: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