Thread: Splitting a number.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    Splitting a number.

    Hey everyone!

    I was working on this program and I came upon a problem.

    When the user inputs:
    85

    I want it to be broken up into "8" and be stored in a variable, and into a "5" and be stored into a variable.

    Another example would be: 74.
    The the variable a = 7;
    and the variable b = 4;

    I thought of everything but I cant figure out a simple way of doing this.

    If you can give any help, I will appreciate it!

    Thank you,

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use the modulo (%) operator. This particular operator returns the remainder of one number divided by another. If you use num%10, you'll get the number in the tens column. You can then divide num by 10 and go num%10 again, perhaps until num is 0, unless you know how many digits the number will have.

    Search the board, this is a common problem.

    [edit] For example: http://cboard.cprogramming.com/showthread.php?t=91037 [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    I understand what you mean. Thank you for your help, I cant believe I couldnt think of something so simple!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  2. Replies: 4
    Last Post: 10-10-2004, 01:01 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM