C++ Program to find the 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++.


The number is: 1234

Even the digit sum is: 2 + 4 => 6

Odd digit sum is: 1 + 3 => 4

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

#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 << "Sum of even digits:" << se;
    cout << "\nSum of odd digits:" << sd;

    return 0;
}

Output:

Enter a number:12345
Sum of even digits:6
Sum of odd digits:9

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.