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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  //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;



  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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    int a, b;
    cout<<"a + b = sum total"<<endl;
    cout<<"Enter value of 'a'"<<endl;
    cin>>a;
    cout<<"Enter value of 'b'"<<endl;
    cin>>b;
    int sumtotal = a + b;
    cout<<a<<" + "<<b<<" = "<<sumtotal<<endl;

    return 0;
}
 


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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//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 <limits>
#include <string>


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<str.length(); i++) {
        std::cout << str.at(i);
        if(i == str.length() - 1) { std::cout << '\n'; }
        else                      { std::cout << ' '; }
    }
}


void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::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.
1
2
3
4
5
6
7
8
9
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
Take your pick ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
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;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
using namespace std;

int main()
{
   int N;
   cout << "Input N: ";
   cin >> N;

   vector<int> 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;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <stack>
using namespace std;

int main()
{
   int N;
   cout << "Input N: ";
   cin >> N;

   stack<int> 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.