sum of digits of an int

Write your question here.
Hi, I am trying to write a code to sum the digits of an integer but it keeps on coming back as a 0:( anything will help, thanks!
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
  #include<iostream>
using namespace std;

int main()
{
	int num = 0;
	int sum = 0;
	int temp = 0;
	int mod = 0;

	cout << "Enter an integer " << endl;
	cin >> num;

	while (mod != 0)
	{
		
		sum = 0;
		temp = num / 10;
		mod = num % 10;

		sum += mod;
		

		
	}

		
	

	cout << "adding " << num << " is equal to " << sum << endl;

	system("Pause");

	return 0;

}
Line 14: You're never going to execute your loop since you initialize mod to 0.
Line 17: You don't want to set sum equal to zero inside the loop. If you do, you will set reset the sum to zero for each iteration.
Line 18: What;s the point of this line? You never use temp.
thank you!
I'm sorry, I corrected it the best I could. It is at least calculating now but something is still wrong:(
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
#include<iostream>
using namespace std;

int main()
{
	int num = 0;
	int sum = 0;
	
	int mod = 1;

	cout << "Enter an integer " << endl;
	cin >> num;
	
	while (mod != 0)
	{
		
		
		num = num / 10;
		mod = num % 10;

		sum += mod ;
		

		
	}

		
	

	cout << "adding " << num << " is equal to " << sum << endl;

	system("Pause");

	return 0;

}
Line 14: You want to continue until num is 0, not mod.
Line 18-19: Calculate mod BEFORE dividing num by 10.
million thanks!
im sorry, one last question:
I extended the program a little to check if divisible by 3, but now its not outputting my num.

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
#include<iostream>
using namespace std;

int main()
{
	int num = 0;
	int sum = 0;
	int temp = 0;
	
	int mod = 1;

	cout << "Enter an integer " << endl;
	cin >> num;

	
	
	while (num != 0)
	{
		
		mod = num % 10;
		num = num / 10;
		

		sum += mod;
		

		
	}

	

	cout << "sum = " << sum << endl;
	temp = sum % 3;
	if (temp == 0)
	{
		cout  << num << " is divisible by 3" << endl;
	}
	else
	{
		cout << num << " is NOT divisible by 3" << endl;
	}

	

	
	system("Pause");

	return 0;

}

That's because in your lloop from 17-28 you keep dividing num until it is zero.
When you exit the loop, num is 0.
thank you, I realize that. However if I move my cout to before the loop then im not gonna have the value for sum, so im not sure how to solve that problem
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

int main()
{
	int num = 0;
	int sum = 0;
	int mod = 1;

	std::cout << "Enter an integer " << std::endl;
	std::cin >> num;
	
	std::cout << "Adding " << num; // <--------
	
	while (num != 0) //mod != 0)
	{
		mod = num % 10;
		sum += mod ;
		
		num /= 10;
	}

    std::cout << " is equal to " << sum << std::endl;  // <--------
    return 0;
}
Last edited on
@kemort

That's not going to work correctly for the reason I pointed out earlier.
Consider the number 102. The sum should be 3, but your loop will stop because the second digit is 0.

closed account (48T7M4Gy)
oops I missed what u wrote. The test is num != 0
Topic archived. No new replies allowed.