Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. get the last # from a 2 digit sum

get the last # from a 2 digit sum

Scheduled Pinned Locked Moved C / C++ / MFC
comtutorialquestion
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I InternetMill

    Hey, Is there a way that you can take the last number (one's digit) from a sum of 2 numbers added together? For example if I have numbers like 4, 8 that = 12. I only want to take the 2 though. Is this possible? Thanks Matt Matt Millican http://www.internetmill.com

    C Offline
    C Offline
    Chris Losinger
    wrote on last edited by
    #2

    #include < math.h>
    int a = 8;
    int b = 4;
    int lastDigit = (a + b) - (int)pow(10, (int)log10(a + b)));

    -c


    When history comes, it always takes you by surprise.

    Bobber!

    N 1 Reply Last reply
    0
    • I InternetMill

      Hey, Is there a way that you can take the last number (one's digit) from a sum of 2 numbers added together? For example if I have numbers like 4, 8 that = 12. I only want to take the 2 though. Is this possible? Thanks Matt Matt Millican http://www.internetmill.com

      N Offline
      N Offline
      Nitron
      wrote on last edited by
      #3

      int num = 4 + 8;
      CString szNumber = "";
      szNumber.Format("%d", num);
      int ones_col = atoi(szNumber.Right(1));

      - Nitron


      "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

      1 Reply Last reply
      0
      • C Chris Losinger

        #include < math.h>
        int a = 8;
        int b = 4;
        int lastDigit = (a + b) - (int)pow(10, (int)log10(a + b)));

        -c


        When history comes, it always takes you by surprise.

        Bobber!

        N Offline
        N Offline
        Nitron
        wrote on last edited by
        #4

        Chris Losinger wrote: int lastDigit = (a + b) - (int)pow(10, (int)log10(a + b))); :omg: I guess I didn't think of it that way ;P - Nitron


        "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

        C 1 Reply Last reply
        0
        • N Nitron

          Chris Losinger wrote: int lastDigit = (a + b) - (int)pow(10, (int)log10(a + b))); :omg: I guess I didn't think of it that way ;P - Nitron


          "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #5

          :) i like a solution that's more complicated than the problem it solves. -c


          When history comes, it always takes you by surprise.

          Bobber!

          T 1 Reply Last reply
          0
          • I InternetMill

            Hey, Is there a way that you can take the last number (one's digit) from a sum of 2 numbers added together? For example if I have numbers like 4, 8 that = 12. I only want to take the 2 though. Is this possible? Thanks Matt Matt Millican http://www.internetmill.com

            R Offline
            R Offline
            Roger Allen
            wrote on last edited by
            #6

            Surely you can use the MOD operator?

            int b = 4;
            int c = 8;
            int a = ((b + c) % 10);

            Roger Allen Sonork 100.10016 Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003

            1 Reply Last reply
            0
            • I InternetMill

              Hey, Is there a way that you can take the last number (one's digit) from a sum of 2 numbers added together? For example if I have numbers like 4, 8 that = 12. I only want to take the 2 though. Is this possible? Thanks Matt Matt Millican http://www.internetmill.com

              J Offline
              J Offline
              Joao Paulo Figueira
              wrote on last edited by
              #7

              Try:

              int a = 4;
              int b = 8;
              int c = (a + b) % 10;

              I think it is the fastest.

              M 1 Reply Last reply
              0
              • C Chris Losinger

                :) i like a solution that's more complicated than the problem it solves. -c


                When history comes, it always takes you by surprise.

                Bobber!

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #8

                I'm keeping my mouth shut. :laugh: Tim Smith I'm going to patent thought. I have yet to see any prior art.

                C 1 Reply Last reply
                0
                • J Joao Paulo Figueira

                  Try:

                  int a = 4;
                  int b = 8;
                  int c = (a + b) % 10;

                  I think it is the fastest.

                  M Offline
                  M Offline
                  mijarral
                  wrote on last edited by
                  #9

                  And also it is the best way to do this.

                  1 Reply Last reply
                  0
                  • T Tim Smith

                    I'm keeping my mouth shut. :laugh: Tim Smith I'm going to patent thought. I have yet to see any prior art.

                    C Offline
                    C Offline
                    Chris Losinger
                    wrote on last edited by
                    #10

                    come on, spit it out. i'd hate for you to have to hold back. -c


                    Image tools: ThumbNailer, Bobber, TIFFAssembler

                    T 1 Reply Last reply
                    0
                    • C Chris Losinger

                      come on, spit it out. i'd hate for you to have to hold back. -c


                      Image tools: ThumbNailer, Bobber, TIFFAssembler

                      T Offline
                      T Offline
                      Tim Smith
                      wrote on last edited by
                      #11

                      Nah, was just going to be my same rant about some of the overly complex solutions to simple problems. It is the same rant I have always had. I wanted to spare people the misery. :laugh: Tim Smith I'm going to patent thought. I have yet to see any prior art.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups