Sum of 6 integers - How does it work?

This is the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


#include <iostream>

using namespace std;

int main()
{
	int n, sum;
	cout << "Enter a six-digit integer: ";
	cin >> n;
	sum = n%10 + n/10%10 + n/100%10 + n/1000%10 + n/10000%10;

	cout << "The sum of the digits of " << n << " is " << sum << endl;

	system("pause");
	return 0;

}


The program executes fine, but how does it exactly work? Can someone offer some insight into what exactly the operators assigned to the "int sum" do? Thank you.
Seems like you have a problem understanding the logic behind the program. I think you are confused about the modulus operator %.

The program is basically calculating what is the last digit and adding it to the one before that and the one before that etc. etc.
Last edited on
Oh yes..thanks...that's it.
Topic archived. No new replies allowed.