### Converting numbers into Digits & then SUm

We are only 5 chapters into our textbook & I need to ask a user to input a number and then output it as digits.

QuESTION =
(1)
How do you convert an integer into a string and then convert that string into different digits ?

(2) Is there a better number, this program has to work for any number input, NOT A SPECIFIC number of digits as I have seen in many examples.

Keep in my mind I do not know advanced syntax and we are supposed to be able to do this having gone through 5 chapters in the textbook.

 ``123456789101112131415161718192021222324252627282930313233`` `````` //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. #include #include #include #include using namespace std; int main() { int integer = 0; int sum = 0; int digits = 0; string value; cout << "Please enter any number: "; cin >> integer; cout << " You entered: " << integer << endl; string value = to_string(integer); sum = digits + digits; cout << " The sum of these digits is:" << digits; return 0; }``````
//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.

#include<iostream>
#include<string>
#include<vector>
#include <sstream>

using namespace std;

int main()
{
int integer = 0;
int sum = 0;
int digits = 0;
string value;
char ch;

cout << "Please enter any number: ";
cin >> integer;
//Convert int into a string ( this syntax makes no fucking sense)
stringstream ss;
ss << integer;
string str = ss.str();
// It actually worked for once, except it did not out put it with space
// so technically it does not work

cout << " You entered: " << str << endl;

sum = digits + digits;
cout << " The sum of these digits is:" << digits;

return 0;
}
Hey a lot of the code seems rather unnecessary, this is how you could script your variables to get a sum, then print.

 ``12345678910111213141516171819`` ``````#include #include using namespace std; int main() { int a, b; cout<<"a + b = sum total"<>a; cout<<"Enter value of 'b'"<>b; int sumtotal = a + b; cout<

Hope this helped bud :)

end if you would like to be able to input decimal/negative values, change

'int a, b;' to 'double a, b;' and

'int sumtotal = a+b;' to 'double sumtotal = a+b;'
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````//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. #include #include #include void printDigitByDigit(const std::string& str); void waitForEnter(); int main() { std::cout << "Could you please give me a number? "; std::string first; std::getline(std::cin, first); std::cout << "Could you please give me another number? "; std::string second; std::getline(std::cin, second); std::cout << "The first number you gave me was: "; printDigitByDigit(first); std::cout << "The second number you gave me was: "; printDigitByDigit(second); std::cout << "Their sum is " << std::stoi(first) + std::stoi(second) << '\n'; waitForEnter(); return 0; } void printDigitByDigit(const std::string& str) { for(size_t i=0; i::max(), '\n'); }``````

 ```Could you please give me a number? 84512351 Could you please give me another number? -23154265 The first number you gave me was: 8 4 5 1 2 3 5 1 The second number you gave me was: - 2 3 1 5 4 2 6 5 Their sum is 61358086 Press ENTER to continue...```

.
Last edited on
You don't have to use strings to sum the digits of an integer. You can extract the one's digit with the modulus operator(%) and then divide the number by 10. Keep repeating this process until the number is 0.
 ``123456789`` ``````int sum_digits( int n ) { int sum{}; while( n != 0 ) { sum += n % 10; n /= 10; } return sum; }``````

For example, say we have the number 574.
574 % 10 is 4. sum += 4 -> sum is 4. 574 / 10 is 57 (integer division).
57 % 10 is 7. sum += 7 -> sum is 11. 57 / 10 is 5.
5 % 10 is 5. sum += 5 -> sum is 16. 5 / 10 is 0.
Loop terminates and we return 16.
.
Last edited on
.
Last edited on
.
Last edited on
.
Last edited on
.
Last edited on
.
Last edited on
Sorry for the spam post. Was having issues submitting.
Last edited on

 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main() { int N; cout << "Input N: "; cin >> N; int base = 1; while ( N >= base * 10 ) base *= 10; int sum = 0; while ( base >= 1 ) { int digit = N / base; cout << digit << " "; sum += digit; N -= digit * base; base /= 10; } cout << "\nSum of digits = " << sum; }``````

 ``12345678910111213141516171819202122232425`` ``````#include #include using namespace std; int main() { int N; cout << "Input N: "; cin >> N; vector digits; while ( N > 0 ) { digits.push_back( N % 10 ); N /= 10; } int sum = 0; for ( int i = digits.size() - 1; i >= 0; i-- ) { cout << digits[i] << " "; sum += digits[i]; } cout << "\nSum of digits = " << sum; }``````

 ``123456789101112131415161718192021222324252627`` ``````#include #include using namespace std; int main() { int N; cout << "Input N: "; cin >> N; stack digits; int sum = 0; while ( N > 0 ) { int i = N % 10; digits.push( i ); sum += i; N /= 10; } while( !digits.empty() ) { cout << digits.top() << " "; digits.pop(); } cout << "\nSum of digits = " << sum; }``````
I definitely misunderstood the question.
Sorry, ninjanewbie.
Topic archived. No new replies allowed.