find number of occurances of specific digit with a counter in C++ - c++

I have a large number whereby i want to find the number of occurances of a specified digit. I wonder is using a counter will work. My code as follows:
#include <iostream>
using namespace std;
int main( )
{
int number;
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;
int n10;
int digit;
int digitCounter = 0;
cout << "Please enter a number between 100 to 2000000000." << endl;
cin >> number;
cout << "Please enter a digit that you want to find the number of occurances." << endl;
cin >> digit;
if (number > 0)
{
n1 = number % 10;
n2 = (number/10) % 10;
n3 = (number/100) % 10;
n4 = (number/1000) % 10;
n5 = (number/10000) % 10;
n6 = (number/100000) % 10;
n7 = (number/1000000) % 10;
n8 = (number/10000000) % 10;
n9 = (number/100000000) % 10;
n10 = (number/100000000) % 10;
if (n1 == digit)
{ digitCounter++;}
else if (n2 == digit)
{ digitCounter++;}
else if (n3 == digit)
{ digitCounter++;}
else if (n4 == digit)
{ digitCounter++;}
else if (n5 == digit)
{ digitCounter++;}
else if (n6 == digit)
{ digitCounter++;}
else if (n7 == digit)
{ digitCounter++;}
else if (n8 == digit)
{ digitCounter++;}
else if (n9 == digit)
{ digitCounter++;}
else if (n10 == digit)
{ digitCounter++;}
cout<< "The total number of occurances of " << digit << " in " << number <<" is "<<digitCounter<< endl;
}
else
cout<< "You have entered an invalid number."<<endl;
system("pause");
return 0;
}
However the counter is not working. Can someone advise what wet wrong?
Any help is really appreciated, thanks.

You can cast your number into a string after that you search the digit in the string it's simpler.
Or you can read a character string (number) and a character (digit) and then you do like that :
char number[20], digit;
int count = 0, i;
printf("\nEnter a string : ");
scanf("%s", &number);
printf("\nEnter the character to be searched : ");
scanf("%c", &digit);
for (i = 0; number[i] != '\0'; i++) {
if (number[i] == digit)
count++;
}
if (count == 0)
printf("\nCharacter '%c'is not present", digit);
else
printf("\nOccurence of character '%c' : %d", digit, count);`

Your Else If's need to be If's. As it stands now you only go through one decision statement. As soon as it finds a match you're out.
#include <iostream>
using namespace std;
int main()
{
int number;
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;
int n10;
int digit;
int digitCounter = 0;
cout << "Please enter a number between 100 to 2000000000." << endl;
cin >> number;
cout << "Please enter a digit that you want to find the number of occurances." << endl;
cin >> digit;
if (number > 0)
{
n1 = number % 10;
n2 = (number / 10) % 10;
n3 = (number / 100) % 10;
n4 = (number / 1000) % 10;
n5 = (number / 10000) % 10;
n6 = (number / 100000) % 10;
n7 = (number / 1000000) % 10;
n8 = (number / 10000000) % 10;
n9 = (number / 100000000) % 10;
n10 = (number / 100000000) % 10;
if (n1 == digit)
{
digitCounter++;
}
if (n2 == digit)
{
digitCounter++;
}
if (n3 == digit)
{
digitCounter++;
}
if (n4 == digit)
{
digitCounter++;
}
if (n5 == digit)
{
digitCounter++;
}
if (n6 == digit)
{
digitCounter++;
}
if (n7 == digit)
{
digitCounter++;
}
if (n8 == digit)
{
digitCounter++;
}
if (n9 == digit)
{
digitCounter++;
}
if (n10 == digit)
{
digitCounter++;
}
cout << "The total number of occurances of " << digit << " in " << number << " is " << digitCounter << endl;
}
else
cout << "You have entered an invalid number." << endl;
system("pause");
return 0;
}

try this
#include <iostream>
int main(int argc, char **argv) {
unsigned long long large(0);
int digitToFind(0);
std::cout << "enter a large number [100 to 2000000000]" << std::endl;
std::cin >> large;
if (large < 100 || large > 2000000000) {
std::cout << "invalid input." << std::endl;
return -1;
}
std::cout << "enter the digit to find" << std::endl;
std::cin >> digitToFind;
if (digitToFind < 0 || digitToFind > 9) {
std::cout << "invalid input." << std::endl;
return -1;
}
std::size_t counts[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
while (large > 0) {
int rem = large % 10;
counts[rem]++;
large /= 10;
}
std::cout << "number of occurrances of " << digitToFind << " is "
<< counts[digitToFind] << std::endl;
std::cout << "press enter to continue" << std::endl;
std::cin.get();
return 0;
}

Some of the sample codes contain another flaw: if the number is small then we get more zeroes than necessary...we cannot assume that the number is necessarily big.
This works for each number, in Python only because I'm more used to Python at the moment, it is straightforward to convert it to C:
N = 1230533007
digitToFind = 3
digitCount = 0
while N > 0:
d = N % 10
if d == digitToFind:
digitCount += 1
N //= 10
print digitCount

Related

How do I remove comma after last number/output loop in C++

My Code:
#include <iostream>
using namespace std;
void result(int test)
{
for (int num = 1; num <= test; num++)
{
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
cout << ",";
}
}
int main()
{
int test = 100;
result(test);
return 0;
}
My Output:
1,2,love ,4,extraextra,love ,7,8,love ,extraextra,11,love ,13,14,extra,16,17,love ,19,extraextra,love ,22,23,love ,extraextra,26,love ,28,29,extra,31,32,love ,34,extraextra,love ,37,38,love ,extraextra,41,love ,43,44,extra,46,47,love ,49,extraextra,love ,52,53,love ,extraextra,56,love ,58,59,extra,61,62,love ,64,extraextra,love ,67,68,love ,extraextra,71,love ,73,74,extra,76,77,love ,79,extraextra,love ,82,83,love ,extraextra,86,love ,88,89,extra,91,92,love ,94,extraextra,love ,97,98,love ,extraextra,
My question:
How to delete only the very last comma?
Add a condition for the last value of num. The full code would be:
#include <iostream>
using namespace std;
void result(int test)
{
for (int num = 1; num <= test; num++)
{
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
if(num < test) cout << ","; // Here you add the condition
}
cout << endl; // Allows for flushing the output (Thanks #bruno)
}
int main()
{
int test = 100;
result(test);
return 0;
}
In case you really do not like the additional test of num visible in
for (int num = 1; num <= test; num++)
{
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
if (num < test)
cout << ',';
}
and
for (int num = 1; num <= test; num++)
{
if (num != 1)
cout << ',';
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
}
you can avoid that explicit test doing :
const char * sep = "";
for (int num = 1; num <= test; num++)
{
cout << sep;
sep = ",";
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
}
but probably this is less readable and a (very) little more expensive at the execution
Your loop runs for the last time at num == test, so just print commas for num less than test or num not equal to test.
i.e. simply replace cout << ","; with:
if(num < test) cout << ",";
or
if(num != test) cout << ",";
After having read all the suggestion and the constructive critics, I modified my answer in the following way:
#include <iostream>
using namespace std;
void singlecheck(int num){
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
}
void result(int test)
{
if(test>0){
for (int num = 1; num <test; num++){
singlecheck(num);
cout << ", ";
}
singlecheck(test);
cout << endl;
}
}
int main()
{
int test = 15;
result(test);
return 0;
}
I added one more function, which is doing the "test" job, while the cycle is done in another function. Now the result is correct also with input 0: I added one if at the beginning. In this way, only one if-check has to be done instead of a total of test if-checks, which was my principal concern. Previous version has repeated code which is not a good programming practice.

How do i continue this while

Im stuck with this program. What program does is to take a integer from user and display all number that are left after cutting the even numbers.
int main(){
long n, minder=0;
int cdonr, power=1;
cout<<"Give a positive number "<<endl;
cin>>n;
while (n>0){
cdonr=n%10;
if(cdonr % 2 != 0){
minder=minder+cdonr*power;
power=power*10;
}
n=n/10;
}
cout<<"The number that is left after all even number " << endl;
cout<<minder<<endl;
cout<<"Give a positive nr "<<endl;
cin>>n;
}
Can someone help me with this while because after it split the first numbers it gives no response at the second one.
Thanks to people on this community this is the answer
#include<iostream>
using namespace std;
int main() {
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
//always zero so we need a nested while loop
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
//have to set values to default so they do not hold previous values
minder = 0;
cdonr = 0;
power = 1;
//we can reprompt user for a value
//n will get set and it validates with the outer while loop
//this allows it to run as many times as valid inputs
cout << "Give a positive number " << endl;
cin >> n;
}
}
Another answer as requested by user, exits upon == 0, <= 0, and not a number
#include<iostream>
using namespace std;
int main() {
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
//always zero so we need a nested while loop
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
//have to set values to default so they do not hold previous values
minder = 0;
cdonr = 0;
power = 1;
//we can reprompt user for a value
//n will get set and it validates with the outer while loop
//this allows it to run as many times as valid inputs
cout << "Give a positive number " << endl;
cin >> n;
}
}
Try this, it is not infinite and you can do it as many times as you want by calling the function.
#include<iostream>
using namespace std;
void function(long n, long minder, int cdonr, int power) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
}
int main() {
long n, minder = 0;
int cdonr, power = 1;
//can add a while loop here or just use a for
// if you know how many times you want
cout << "Give a positive number " << endl;
cin >> n;
if(n > 0){
function(n, minder, cdonr, power);
}
//probably add an else here in case of == 0 or < 0
// or just loop back if while
}
If you really want to do something forever, loop on it.
i.e. put your "function" to be repeated in a loop.
(And consider pulling it out to be an actual function)
int main() {
using namespace std;
while (true)
{
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr*power;
power = power * 10;
}
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
}
}

C++ Arabic to roman numeral converter - of input validation loops

The purpose of this program is to take an input of an arabic number and return the equivelant roman numeral. it needs to reject input less than 0 and greater than 3999, but also end on an input of ANY NEGATIVE NUMBER. Also needs an input fail catch for entering a letter instead of an integer. (completely clueless on this part?!)
My question is what order should these conditional statement be in so that the program doesnt just return invalid input for a negative number, it ends the program.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
string convert(int digit, string low, string mid, string high);
int main()
{
const int MAX_INPUT = 3999, MIN_INPUT = 0, // These constants hold high and low integer numbers,
ARRAY_SIZE = 4; // and the array size declarator.
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
do {
cout << "Enter a negative number to end the program.\n";
cout << "Enter an arabic number between 1 and 3999: ";
cin >> userNum;;
if (userNum < 0)
{
cout << "Exiting program:";
break;
while (userNum <= MIN_INPUT || userNum >= MAX_INPUT)
{
cout << "\nInvalid Value. Number must be between 1 and 3999: ";
cin >> userNum;
}
}
int thous = userNum / 1000;
cout << thous;
int hund = userNum % 1000 / 100;
cout << "hundreds:" << hund;
int tens = userNum % 100 / 10;
cout << "tens:" << tens;
int ones = userNum % 10 / 1;
cout << "Ones: " << ones << endl;
answers[0] = convert(thous, "M", "M", "M");
answers[1] = convert(hund, "C", "D", "M");
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << endl << answers[1] << endl << answers[2];
cout << endl << answers[3] << endl;
} while (userNum > 0);
system("PAUSE");
return 0;
}
string convert(int digit, string low, string mid, string high)
{
cout << digit << endl;
if (digit == 1)
{
return low;
}
if (digit == 2)
{
return low + low;
}
if (digit == 3)
{
return low + low + low;
}
if (digit == 4)
{
return low + mid;
}
if (digit == 5)
{
return mid;
}
if (digit == 6)
{
return mid + low;
}
if (digit == 7)
{
return mid + low + low;
}
if (digit == 8)
{
return mid + low + low + low;
}
if (digit == 9)
{
return low + high;
}
if (digit == 0)
{
return "";
}
}
A possible solution with minimal refactoring of your current code:
int main()
{
const int MAX_INPUT = 3999, MIN_INPUT = 0, // These constants hold high and low integer numbers,
ARRAY_SIZE = 4; // and the array size declarator.
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
do {
cout << "Enter a negative number to end the program.\n";
cout << "Enter an arabic number between 1 and 3999: ";
//cin >> userNum;;
while(!(cin >> userNum) || (userNum < 1 || userNum > 3999)){
if (userNum < 0)
{
cout << "Exiting program:";
return 0;
} else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nInvalid Value. Number must be between 1 and 3999. Try again ";
}
}
int thous = userNum / 1000;
cout << thous;
int hund = userNum % 1000 / 100;
cout << "hundreds:" << hund;
int tens = userNum % 100 / 10;
cout << "tens:" << tens;
int ones = userNum % 10 / 1;
cout << "Ones: " << ones << endl;
answers[0] = convert(thous, "M", "M", "M");
answers[1] = convert(hund, "C", "D", "M");
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << endl << answers[1] << endl << answers[2];
cout << endl << answers[3] << endl;
} while (userNum > 0);
system("PAUSE");
return 0;
}
In sudo code:
while (inputVar >= 0) {
try {
inputVar = stoi(inputVar)
} catch {
print("Input error")
}
if in range(min to max) {
roman = romanEquiv(inputVar)
print(roman)
} else {
print("Invalid entry.")
}
}
Try-catch blocks (for catching exceptions you know your program should handle)
std::stoi a function in the cpp library to try and convert a string to an integer
Hope this helps, let me know how it goes.
I revised the beginning of the main method and the beginning of the while loop.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define MAX_INPUT 3999 // These constants hold high and low integer numbers,
#define MIN_INPUT 1
#define ARRAY_SIZE 4 // and the array size declarator.
string convert(int digit, string low, string mid, string high);
int main()
{
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
string strUserNum;
do {
cout << "Enter a negative number to end the program.\n";
cout << "Enter an arabic number between 1 and 3999: ";
cin >> strUserNum;
userNum = std::stoi(strUserNum);
if (userNum == 0 || userNum > MAX_INPUT)
{
cout << "\nInvalid Value. Number must be between 1 and 3999: " << endl;
continue;
}
else if (userNum < MIN_INPUT)
{
cout << "Exiting program:";
break;
}
int thous = userNum / 1000;
cout << "thousands: " << thous;
int hund = userNum % 1000 / 100;
cout << " hundreds: " << hund;
int tens = userNum % 100 / 10;
cout << " tens: " << tens;
int ones = userNum % 10 / 1;
cout << " ones: " << ones << endl;
answers[0] = convert(thous, "M", "M", "M");
answers[1] = convert(hund, "C", "D", "M");
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << endl << answers[1] << endl << answers[2];
cout << endl << answers[3] << endl;
} while (userNum >= 0);
system("PAUSE");
return 0;
}
string convert(int digit, string low, string mid, string high)
{
cout << digit << endl;
if (digit == 1)
{
return low;
}
if (digit == 2)
{
return low + low;
}
if (digit == 3)
{
return low + low + low;
}
if (digit == 4)
{
return low + mid;
}
if (digit == 5)
{
return mid;
}
if (digit == 6)
{
return mid + low;
}
if (digit == 7)
{
return mid + low + low;
}
if (digit == 8)
{
return mid + low + low + low;
}
if (digit == 9)
{
return low + high;
}
if (digit == 0)
{
return "";
}
}
But I would like to also provide an alternative implementation of the string conversion ( from http://rosettacode.org/wiki/Roman_numerals/Encode#C.2B.2B ):
std::string to_roman(unsigned int value)
{
struct romandata_t { unsigned int value; char const* numeral; };
const struct romandata_t romandata[] =
{
{1000, "M"}, {900, "CM"},
{500, "D"}, {400, "CD"},
{100, "C"}, { 90, "XC"},
{ 50, "L"}, { 40, "XL"},
{ 10, "X"}, { 9, "IX"},
{ 5, "V"}, { 4, "IV"},
{ 1, "I"},
{ 0, NULL} // end marker
};
std::string result;
for (const romandata_t* current = romandata; current->value > 0; ++current)
{
while (value >= current->value)
{
result += current->numeral;
value -= current->value;
}
}
return result;
}
I would suggest you this C++14 code:
#include <utility>
#include <string>
auto toRoman(unsigned arabic)
{
constexpr auto table = {
std::make_pair(1000u, "M"),
std::make_pair(900u, "CM"),
std::make_pair(500u, "D"),
std::make_pair(400u, "CD"),
std::make_pair(100u, "C"),
std::make_pair(90u, "XC"),
std::make_pair(50u, "L"),
std::make_pair(40u, "XL"),
std::make_pair(10u, "X"),
std::make_pair(9u, "IX"),
std::make_pair(5u, "V"),
std::make_pair(4u, "IV"),
std::make_pair(1u, "I")
};
std::string roman;
for (auto& pair: table)
{
while (pair.first <= arabic)
{
arabic -= pair.first;
roman += pair.second;
}
}
return roman;
}

Binary math programming

I have been asked to create a program that takes 2 binary numbers then outputs the result of adding, subtracting and multiplying them. My code works for addition but when I try to do subtraction or multiplication, the output is not as expected. The cout statement is displayed with each digit and several of the digits are incorrect. Could anyone point to flaws in my code?
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
long num1, num2;
int choice;
int i = 0, r = 0, sum[20];
cout << "Enter the first binary number: "; //prompts user to input binary values to perform arithmetic on
cin >> num1;
cout << "Enter the second binary number: ";
cin >> num2;
cout << "To add the numbers press 1, to subtract the numbers press 2," << endl; //prompts user to choose which operation to perform
cout << "to multiply the numbers press 3" << endl;
cin >> choice;
if (choice != 1 & choice != 2 & choice != 3) //if statement to test for valid input
{
cout << "Please enter a valid choice: ";
cin >> choice;
}
if (choice == 1)
{
while (num1 != 0 || num2 != 0) //if user chooses 1, perform binary arithmetic
{
sum[i++] = (num1 % 10 + num2 % 10 + r) % 2;
r = (num1 % 10 + num2 % 10 + r) / 2;
num1 = num1 / 10;
num2 = num2 / 10;
}
if (r != 0)
sum[i++] = r;
--i;
cout << "The sum of the two numbers is: ";
while (i >= 0)
cout << sum[i--];
cout << ". ";
}
if (choice == 2)
{
while (num1 != 0 || num2 != 0) //if user chooses 2, perform binary subtraction
{
int i = 0, r = 0, diff[20];
diff[i++] = (num1 % 10 - num2 % 10 + r) % 2;
r = (num1 % 10 - num2 % 10 + r) / 2;
num1 = num1 / 10;
num2 = num2 / 10;
if (r != 0)
diff[i++] = r;
--i;
cout << "The difference of the two numbers is: ";
while (i >= 0)
cout << diff[i--];
cout << ". ";
}
}
if (choice == 3) //if user chooses 3, perform binary multiplication
{
while (num1 != 0 || num2 != 0)
{
int i = 0, r = 0, product[20];
product[i++] = (num1 % 10 * num2 % 10 + r) % 2;
r = (num1 % 10 * num2 % 10 + r) / 2;
num1 = num1 / 10;
num2 = num2 / 10;
if (r != 0)
product[i++] = r;
--i;
cout << "The product of the two numbers is: ";
while (i >= 0)
cout << product[i--];
cout << ". ";
}
}
system("pause");
return 0;
}
You may like to try something
int binary2decimal(int n) /* Function to convert binary to decimal.*/
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}
int main()
{
....
....
if(!num1 || !num2)
{
cout << "Result = 0";
exit(0);
}
.....
switch(choice)
{
case 1:
int r = binary2decimal(num1) + binary2decimal(num2);
cout << "Result = " << r;
break;
case 2:
int r = binary2decimal(num1) - binary2decimal(num2);
cout << "Result = " << (r>=0) ? r : -r;
break;
case 3:
int r = (num1 & num2) ?
binary2decimal(num1) * binary2decimal(num2) : 0;
cout << "Result = " << r;
break;
default:
cout << "Enter a valid choice next time";
break;
}
.....
}

Classifying digits of an integer value

I spent a day on this code for count even and zero and odd numbers
From long datatype I used a function to send data. Here is the code
#include <iostream>
using namespace std;
void digitCount(long long int &num);
int main ()
{
long long int num;
cout <<"Enter any No. " <<endl;
cin >>num;
cout <<endl;
digitCount(num);
return 0;
}
void digitCount(long long int &num)
{
int e = 0, z = 0, o = 0, x = 0;
for (int i = 0; i <= num; i++)
{
x= num % 10;
if(x == 0)
{
++z;
num = num / 10;
}
else if(x%2==1)
{
++o;
num = num / 10;
}
else
{
++e;
num = num / 10;
}
}
cout << "No of zeros Digits = " << z<< endl;
cout << "No of odd Digits = " << o << endl;
cout << "No of Even Digits = " << e << endl;
}
the problem is when I count odd numbers there is a number missed
for example when i input : 12345
the result is
no of even : 2
no of odd : 2 (should be 3)
no of zero : 0
and here the question :
Write a function that takes as parameter an integer (as a long value) and returns the number of odd, even, and zero digits. Also write a program to test your function. Use pass by reference method.
Instead of the for loop you should use:
while (num > 0)
You're constantly changing num and when it gets to 1 (in your 12345 example), i is at 3. I also modified your digitcount to demonstrate some decent formatting for readable code.
void digitCount(long long int &num) {
int e(0), z(0), o(0), x(0);
while (num > 0) {
x = num % 10;
if (x == 0) {
z++;
}
else if (x % 2 == 1) {
o++;
}
else {
e++;
}
num /= 10;
}
cout << "No of zeros Digits = " << z << endl;
cout << "No of odd Digits = " << o << endl;
cout << "No of Even Digits = " << e << endl;
}
If you believe this solves your problem && is the best answer, please click the checkmark next to this answer. Thanks