Hello i want to sum digits in int can you help me?
ok here is example: i have 6 digit number 123456 i want to sum 123 and 456 i want first 3 digits sum and last 3 digits sum.
#include <iostream>
#include <sstream>
usingnamespace std;
int main()
{
int num = 123456;
int first,second;
string wholeNumber,numOne;
string numbers[3];
stringstream ss;
stringstream secondSs;
ss << num;
ss >> wholeNumber;
// get first number 123
for(int i = 0; i < 3; i++)
{
numbers[i] = wholeNumber.at(i);
secondSs << numbers[i];
}
secondSs >> first;
// clear string stream
secondSs.str("");
secondSs.clear();
// get second part
int j = 0;
for(int i = 3; i <= 5; i++)
{
numbers[j] = wholeNumber.at(i);
secondSs << numbers[j];
j++;
}
secondSs >> second;
// result
cout << " first number " << first << " + " << "second number " << second
<< " = " << first + second << endl;
}
note I read the question wrong I thought you wanted to get the sum of both numbers,but that's actually even simpler.