C++ Program to find sum of even and odd digits of a number


In this program, You will learn how to find sum of even and odd digits of a number in C++.


Number is : 1234

Even sum is : 6

Odd sum is : 4


Example: How to find sum of even and odd digits of a number in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int a, se = 0, sd = 0, r;

    cout << "Enter a Number :";
    cin>>a;

    while (a > 0) {
        r = a % 10;
        if (r % 2 == 0) {
            se = se + r;
        } else {
            sd = sd + r;
        }
        a = a / 10;
    }

    cout << "\nSum of Even Digits :" << se;
    cout << "\nSum of Odd Digits :" << sd;

    return 0;
}

Output:

Enter a Number :1234
Sum of Even Digits :6
Sum of Odd Digits :4