Can anyone help sum of digits

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

using namespace std;

int main() {

	int a = 123456;
	string s = to_string(a);
	int b, c;

	b = stoi(s.substr(0, 1)) + stoi(s.substr(1, 1)) + stoi(s.substr(2,1));
	c = stoi(s.substr(3, 1)) + stoi(s.substr(4, 1)) + stoi(s.substr(5, 1));

	cout << b << " " << c;


	cin.get();
	return 0;
}
thank you but if its posible to get results without functions. using only logical operators loops?
I came up with a solution but Manga beat me to it and done it in a lot less code lol,

easiest solution I thought would be to convert from a string stream to decimals,

again the code is sloppy and bloated but it works.

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
48
49
50
51
52
53
54
55

#include <iostream>
#include <sstream>

using namespace 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.
Last edited on
thank you
The 1+2+3 and 4+5+6?
1
2
3
4
5
6
7
8
9
10
11
int num = 123456;
int low =  num % 1000;
int high = num / 1000;

int lowsum = 0;
while ( low ) {
  lowsum += low % 10;
  low /= 10;
}

// etc 
wow last one is best thank you
Can you explain what does happen there?
everything clear 2 type of division gives firts 3 and last 3 digits but i dont understand what does “while” it works perfectly.
In math we would say that 12345 / 100 equals 123 "whole" and a fraction of 45/100, or in decimal notation 123.45.

The integer division 12345 / 100 returns 123.
The modulus 12345 % 100 returns 45.

In the loop we use % 10 and /= 10.
One returns the last digit and the other returns all but the last digit.

The composite operator /= is a shorthand for:
1
2
3
foo = foo / bar;
//
foo /= bar;

Similarly,
1
2
3
foo = foo + bar;
//
foo += bar;


On every iteration we remove the last digit of low.
The low changes on every iteration; it becomes smaller, and eventually 0.

The loop executes until the low becomes false.
The low is an int, not a bool.
However, there is an implicit conversion from int to bool.

The while ( low ) is like there were:
1
2
3
4
5
6
7
8
9
10
bool cond( int x ) {
  if ( x == 0 ) {
    return false;
  }
  else {
    return true;
  }
}

while ( cond(low) )
You could even put both in the same loop.

1
2
3
4
5
6
7
8
9
int lowsum = 0;
int highsum = 0;
while ( low && high) {
  lowsum += low % 10;
  low /= 10;
  highsum += high % 10;
  high /= 10;

}
Last edited on
You could even put both in the same loop.

Yes, we know that both parts have exactly 3 digits.

However, int Bond = 369007; would not fare so well in that spinner.
True...

And nice James Bond reference. :)
Topic archived. No new replies allowed.