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);