How to check if total odd digits in an integer is odd with C++

1 Answer

0 votes
#include <iostream>

bool TotalOddAreOdd(int n) {
    int odd = 0;
     
    while (n > 0) {
        int reminder = n % 10;
        if (reminder % 2 != 0)
            odd++;
        n = n / 10;
    }
      
    if (odd % 2 != 0)
        return true;
    else
        return false;
}
   
int main()
{
    int n = 19078342;
      
    if (TotalOddAreOdd(n))
        std::cout << "yes";
    else
        std::cout << "no";

    return 0;
}
  
  
  
  
/*
run:
  
no
  
*/

 



answered Sep 8, 2021 by avibootz
...