Write a function that takes as a parameter an integer (as a longlong value) and returns the number of odd, even, and zero digits.Also, write a program to test your function.
Solution
#include <iostream>
using namespace std;
void getDigitCounts(long long int n)
{
int evens = 0, zeros = 0, odds = 0, rem = 0;
while(n>0)
{
rem = n % 10;
if(rem == 0)
{
++zeros;
}
else if(rem%2==1)
{
++odds;
}
else
{
++evens;
}
n/=10;
}
cout << “Count of zeros Digits : ” << zeros<<endl;
cout << “Count of odd Digits : “<< odds <<endl;
cout << “Count of Even Digits : ” << evens <<endl;
}
int main ()
{
long long int n;
cout <<“Enter a number” <<endl;
cin >>n;
getDigitCounts(n);
return 0;
}
OUTPUT :
“D:ACoding
OR
OR