Write Function Takes Parameter Integer Long Long Value Returns Number Odd Even Zero Digits Q37154329

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 CODE BLOCKSISampleCpplbin ReleaselSampleCpp.exe Enter a number 10203045 Count of zeros Digits 3 Count of odd Digi

“D:ACoding

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.