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. Splitting Up Numbers

Splitting Up Numbers

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
5 Posts 4 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.
  • D Offline
    D Offline
    Diagathon
    wrote on last edited by
    #1

    Hello again, I have a lab assignment in my C++ class that asks me to do the following: -Create an application that prompts the user for two 3-digit numbers, then display the result in the following format: ex. 456 x 433 --------- 1368 13680 182400 --------- 197448 The problem I am having is the method I'm trying to use. I am using the carry over technique, and I need a way to split up the 3-digit numbers into single numbers, then multiply/carry/add accordingly Anything unclear let me know, Thanks!

    D 1 Reply Last reply
    0
    • D Diagathon

      Hello again, I have a lab assignment in my C++ class that asks me to do the following: -Create an application that prompts the user for two 3-digit numbers, then display the result in the following format: ex. 456 x 433 --------- 1368 13680 182400 --------- 197448 The problem I am having is the method I'm trying to use. I am using the carry over technique, and I need a way to split up the 3-digit numbers into single numbers, then multiply/carry/add accordingly Anything unclear let me know, Thanks!

      D Offline
      D Offline
      Dr Emmett Brown
      wrote on last edited by
      #2

      You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number. For example: 513 % 10 = 3 -> Last digit 513 / 10 = 51 51 % 10 = 1 -> Second digit 51 / 10 = 5 5 % 10 = 5 -> First digit 5 / 10 = 0 -> END Think a bit.

      R 1 Reply Last reply
      0
      • D Dr Emmett Brown

        You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number. For example: 513 % 10 = 3 -> Last digit 513 / 10 = 51 51 % 10 = 1 -> Second digit 51 / 10 = 5 5 % 10 = 5 -> First digit 5 / 10 = 0 -> END Think a bit.

        R Offline
        R Offline
        Robert C Cartaino
        wrote on last edited by
        #3

        Stewie Griffin wrote:

        You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number.

        Yes, typically. But in this case, the program is prompting the user for the numbers so it is already in string form. He can take the input string...

        char[] n1 = "456"; /* or whatever the user enters */

        ...and "split the number" by accessing it character by character. Then covert each character to an int:

        digit0 = n1[0] - '0'; /* or whatever conversion method you choose */
        digit1 = n1[1] - '1';
        /* etc. */

        D D 2 Replies Last reply
        0
        • R Robert C Cartaino

          Stewie Griffin wrote:

          You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number.

          Yes, typically. But in this case, the program is prompting the user for the numbers so it is already in string form. He can take the input string...

          char[] n1 = "456"; /* or whatever the user enters */

          ...and "split the number" by accessing it character by character. Then covert each character to an int:

          digit0 = n1[0] - '0'; /* or whatever conversion method you choose */
          digit1 = n1[1] - '1';
          /* etc. */

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Robert.C.Cartaino wrote:

          ...the program is prompting the user for the numbers so it is already in string form.

          So put them into a numeric variable (e.g., int) instead. :rolleyes:

          "Love people and use things, not love things and use people." - Unknown

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

          1 Reply Last reply
          0
          • R Robert C Cartaino

            Stewie Griffin wrote:

            You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number.

            Yes, typically. But in this case, the program is prompting the user for the numbers so it is already in string form. He can take the input string...

            char[] n1 = "456"; /* or whatever the user enters */

            ...and "split the number" by accessing it character by character. Then covert each character to an int:

            digit0 = n1[0] - '0'; /* or whatever conversion method you choose */
            digit1 = n1[1] - '1';
            /* etc. */

            D Offline
            D Offline
            Diagathon
            wrote on last edited by
            #5

            I originally attempted the char[] approach, but my compiler (Dev-C++) constantly returned an error the minute I attempted to do any type of mathematical operation. And I do actually like the algorithmic approach. I will give it a try. Thanks!

            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