Number program - Sum of Even Digit

What should be the correct way to find the sum of even digit for Line 21?
For example, 123456
sumEvenDigits = 2 + 4 + 6 = 12

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

int main ()
{
	int n;
	int numDigits = 0, numEvens = 0;
	int numOdds = 0, sumDigits = 0;
	int aDigit;
	int sumEvenDigits;
	
	cout << "Enter a positive number: ";
	cin >> n;
	
	while (n > 0)
	{
		aDigit = n % 10;   // Get the last digit
	
		numDigits++;
		sumDigits += aDigit;	
		//sumEvenDigits += sumDigits + numEvens;
		
		if (aDigit % 2)
			++numOdds;
		else
			++numEvens;
		
		n /= 10;
		
		
	}
	
	cout << "The no of digits is " << numDigits << endl;
	cout << "The no of odd digits is " << numOdds << endl;
	cout << "The no of even digts is " << numEvens << endl;
	cout << "The sum of all digits is " << sumDigits << endl;
	cout << "The sum of even digits is " << sumEvenDigits << endl;
	return 0;
} 	
You are not summing the even digits, you need to initialize sumEvenDigits with 0
Topic archived. No new replies allowed.