Thread: Rightmost digits in integer

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Rightmost digits in integer

    Hi,

    For an integer say 123456, how do i extract the rightmost 3 digits (456) from that integer?

    thx

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    int x = 123456;
    int y = x % 1000;
    //Now y is 456
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Quote Originally Posted by XSquared
    Code:
    int x = 123456;
    int y = x % 1000;
    //Now y is 456
    thx!

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    typecasting to determine truncation

    Oh modulus the beautiful.... Xsquared beat me to it.
    Code:
    #include <vector>
    ...
    ...
    vector <int> breakdown(int number)
    {
     vector<int> buffer;
     while(true)
     {
      if(number / 10 == 0)
      { 
        buffer.push_back(number);
        break;
      }
      buffer.push_back(number % 10);
      number /= 10;
     }
      return buffer;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM