hello,i must solve this problem : "Sum all even digits of a given number. That's what i've got so far.

``````#include <iostream>
using namespace std;
int main() {
int sum = 0 , num;
cin>>num;
while(num > 0)
{
sum+=num%10;
num/=10;
}
cout<<sum;
return 0;
}``````

the problem is that this program sum all digits (odd & even), but i need to sum even digits... help pls.

## All 3 Replies

When you add num%10 to sum you need to test if its a even number.A number is even if the number % 2 == 0 meaning that the remainder of the number divided by 2 is 0 since all even number are divisible by 2.

``````#include <iostream>
using namespace std;
int main()
{
int sum = 0 , num=10,cnt=0;
//cin>>num;
cout<<"Sum all even digits of a given number  \n\n\n";
cout<<"Number = "<<num<<"\n\n\n";

while(cnt < num)
{
cnt++;
if (cnt % 2 ==0)
{
sum+=cnt;
cout<<cnt<<" + ";
}

}
cout<<"  = "<<sum<<" \n\n\n";
return 0;
}``````
``````#include <cstdlib>
#include <iostream>
#include <numeric>
#include <stdio.h>

using namespace std;

{
return sum + (character - '0');
}

int main()
{
cout << "enter number: ";
string num;
cin >> num;

cout << "Total: "