-1

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6, output the individual digits of 8030 as 80 3 0, output the individual digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of 4000 as 4 0 0 0, and output the individual digits of -2345 as 2 3 4 5. I am confused on how the math in the DO-While loop works. Need some help explaining.

#include<iostream>
#include<cmath>

using namespace std;

//Main Function
int main()
{
    //Declare Variables
    int i, num, digit, sum = 0, tempNum, div;

    //Input number
    cout << "Please enter an integer number: ";
    cin >> num;

    //Convert negative values to absolute, as in |-5| = 5
    tempNum = abs(num);

    //Display indiviudal digits
    cout << "The digits are: ";
    i = -1;
    while (tempNum !=0)
    {
        i++;
        tempNum /= 10;
    }
    div = pow(10, i);
    tempNum = abs(num);
    do
    {
        digit = tempNum / div;
        tempNum = tempNum % div;
        sum = sum + digit;
        div = div / 10;
        cout << digit << " ";
    } while (div > 0);
MaciG13
  • 13
  • 4
  • 2
    What specifically confuses you? Can you elaborate more? [Edit] your question and do so please. – πάντα ῥεῖ Jun 23 '21 at 19:21
  • I understand the first while loop. Its dividing the temporary number by 10 until it becomes 0. Then i=13, it loops through that many times. Then I do not understand why raising 10^13. After that I do not understand how the math is working or necessary, – MaciG13 Jun 23 '21 at 19:25
  • Suggestion: Don't comment the obvious stuff. In general, prefer descriptive code to comments, and save the comments for when the code gets ugly or you need a reminder. `//Display indiviudal digits` isn't a bad comment, but if you need a `//Declare Variables` marking a bunch of variable definitions, programming might not be a good career choice. – user4581301 Jun 23 '21 at 19:40
  • Side note: It is often better to define variables close to where they are used rather than in one big block. The keeping the distance between the variable and the usage helps lend context to the variable, prevents readers from having to jump around in the code looking for definitions, and allows you to minimize the scope of each variable which in turn reduces the likelihood of some really tricky bugs. – user4581301 Jun 23 '21 at 19:43
  • Thank you. My proffessor ask us to include that. I will not add that again. – MaciG13 Jun 23 '21 at 19:47
  • 1
    Warning: `pow` computes in floating point and [floating point numbers are imprecise](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). Often what you think is 100000 turns out to be represented as a close-enough 99999.9999999999, which gets chopped down to 99999 when converted back into an integer, resulting in an off-by-one error. It's usually better to stay in integers the whole way and multiply the number out. Often faster, too, since it doen't have to deal with the code needed to compute nasty stuff like e to the power of pi. – user4581301 Jun 23 '21 at 19:48
  • In that case, ignore me for now. Do what the professor wants and pass the the class. Even if the professor's making your life harder. If the professor makes life too hard, it probably worth talking with them about it, but in general the most important thing for you to do is graduate with good grades. You can brush up on modern programming style and techniques later. – user4581301 Jun 23 '21 at 19:51
  • I appreciate your time. Thank you! – MaciG13 Jun 23 '21 at 19:53

1 Answers1

0

I'll go line by line:

Each iteration of the loop will effectively "pop" the first digit off of the loop. Let's say the number is 1234 (or 1000 + 200 + 30 + 4). To start the loop off, we'll have div = 1000 (since that's the highest power in the number) and tempNum = 1234

Let's look at a single iteration of the loop:

  1. This operation would normally produce 1.234 (1234 / 1000), but since this is integer math, it'll round down to just 1 (truncating the .234 part). This stores the first digit in the number (in this case digit = 1):
digit = tempNum / div;
  1. This is the actual "pop" and it does so by computing the modulus of 1234 and 1000. This has the same effect as tempNum - div * floor(tempNum / div), or 1234 - 1000 * floor(1234 / 1000) = 1234 - 1000 * 1 = 234. After this is done, we're left with tempNum = 234:
tempNum = tempNum % div;
  1. Since we want the sum of the digits, we'll need to keep track of it here. Recall that digit = 1 since the first digit in 1234 is 1:
sum = sum + digit;
  1. Since we "popped" the first digit off, the next iteration of the loop needs to have div moved down to the next 10's place. Since the input div was 1000, this operation will decimate it to 100.
div = div / 10;
  1. Print out this digit
cout << digit << " ";

Then the whole loop will repeat, but instead of starting with div = 1000 and tempNum = 1234, we'll have div = 100 and tempNum = 234.

Hopefully that makes sense, I can try to elaborate on any of the points that don't!

mattlangford
  • 1,260
  • 1
  • 8
  • 12