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


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


1234 => 1 + 3 = 4


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

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

int main() {

    int a, sum = 0, r;

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

    while (a > 0) {
        r = a % 10;
        if (r % 2 != 0) {
            sum = sum + r;
        }
        a = a / 10;
    }

    cout << "\nSum of Odd Digits :" << sum;

    return 0;
}

Output:

Enter a Number :12345
Sum of Odd Digits :9