Skip to content

Instantly share code, notes, and snippets.

@mohnoor94
Last active August 20, 2017 21:12
Show Gist options
  • Select an option

  • Save mohnoor94/9c74f643752a433316d11aca516c711d to your computer and use it in GitHub Desktop.

Select an option

Save mohnoor94/9c74f643752a433316d11aca516c711d to your computer and use it in GitHub Desktop.
A full C++ program includes a function that takes as a parameter an integer, and returns the number of odd and even digits.
// www.abukhleif.com
#include <iostream>
using namespace std;
void EvenOdd (int num, int& evens, int& odds) {
evens = odds = 0;
while (num != 0) {
if (num % 2 == 0)
evens++;
else
odds++;
num /= 10;
}
}
int main() {
int number, evenNums, oddNums;
number = -123456789;
EvenOdd(number, evenNums, oddNums);
cout<<"There are (" << evenNums << ") even digits and ("
<< oddNums << ") odd digits in the number ("
<< number <<").";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment