### playing with numbers Hello everyone, so I have a hard one. So far I have the code to run for normal numbers. Here it's asking me to 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. The code also needs negative numbers too. As you can see here, if you were to input 5624; it will come out as "5 6 2 4 Sum:17". My goal is to have -2345 as 2 3 4 5. Could anyone be so kind to explain how this works? Much appreciated coders.

 ``1234567891011121314151617181920212223242526272829303132`` `````` #include using namespace std; int main() { int a = 0; int b  = {0}; int n = 0; int sum = 0; cout << "Show me how many numbers you can give." << endl; cin >> a; cout << endl; while (a > 0) { b[n] = a%10; a /= 10; n++; } for (int i=n; i>0; i--) { printf("%d ", b[i-1]); sum+=b[i-1]; } printf(" Sum: %d ",sum); cin.get(); cin.get(); return 0; } `````` So if someone puts in a negative number, you want it to be converted into a positive number before being split into its digits, is that right?

-Albatross

There's a way of solving this problem that doesn't involve reading an integer from std::cin, but I'll leave you to think about that one. Yes Albatross. When I input -5647, it comes out "Sum: 0"
Last edited on ``12345678910`` ``````//... cout << endl; if (/* a is negative */) { a = /* a but made positive using arithmetic*/; } while (a > 0) //... ``````

Alternatively, instead of the if statement and the code inside it, there is a single function that calculates the absolute value of an integer in C++, which you could use instead. I encourage you to try searching for it. As a hint, it's in the `<cstdlib>` header.

-Albatross Hello Frank5093,

It is not pretty, but more a brute force method, but it should give some ideas.

I am wondering why all the "printf" statements in a C++ program. The "cout" works just as well.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include using namespace std; int main() { bool negative{}; int a = 0; int b  = {0}; int n = 0; int sum = 0; cout << "Show me how many numbers you can give: "; cin >> a; if (a < 0) { negative = true; a = abs(a); } cout << endl; while (a > 0) { b[n] = a%10; a /= 10; n++; } if (negative) { std::cout << "- "; } for (int i =n ; i > 0; i--) { printf("%d ", b[i-1]); sum+=b[i-1]; } if (negative) { sum *= -1; } std::cout << " Sum: " << sum << '\n'; cin.get(); cin.get(); return 0; }``````

I made the changes in the shell program here.

It gives the output of:
 ``` Show me how many numbers you can give: -2345 - 2 3 4 5 Sum: -14 ```

See what you think.

@Albatross,
I think the function that you mean is in the `<cmath>` header file not the `<cstdlib>` file.

Andy
Last edited on Coincidentally the same as the FlyingButtress:
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include using namespace std; int main() { int a = 0; int b  = {0}; int n = 0; int sum = 0; cout << "Show me how many numbers you can give." << endl; cin >> a; if(a < 0) a = -a; cout << endl; while (a != 0) { b[n] = a%10; a /= 10; n++; } for (int i=n; i>0; i--) { printf("%d ", b[i-1]); sum+=b[i-1]; } printf(" Sum: %d ",sum); cin.get(); cin.get(); return 0; }`````` @Handy Andy, about the `int` overload of `std::abs`:

https://en.cppreference.com/w/cpp/numeric/math/abs

EDIT 2: Sorry if I was overly curt with this message. I was in a little bit of a rush when typing it originally. And, full disclosure, I almost typed `<cmath>` as well, and only remembered at the last moment.

-Albatross
Last edited on @Albatross,

Nice to see you back for awhile.

You are right I quick look over the reference section and did not check your link.

It is getting late for me ant that one slipped by.

Thank you for the reminder.

Andy Awesome work coders, I really appreciate this. Thanks a million. Alternative take using recursion:

 ``123456789101112131415161718192021222324`` ``````#include #include void digits(unsigned n, unsigned& sum) { if (n >= 10) digits(n / 10, sum); std::cout << n % 10 << ' '; sum += n % 10; } int main() { int N; unsigned sum {}; std::cout << "Input a number: "; std::cin >> N; digits(std::abs(N), sum); std::cout << "\nSum is: " << sum << '\n'; }``````

 ``` Input a number: -2345 2 3 4 5 Sum is: 14 ```