URGENT!

Hello, I am having trouble trying to figure out how I need to do this problem for my C++ class.

The assignment is asking me to write a program that will count the number of EVEN digits in a given number.

This is my first Programming class so please bare with me.

My current code is below:


int countEven (int n);

int even_count = 0;

int n;

while (n > 0)

{

int rem = n % 10;

if (rem % 2 == 0)

even_count++;

}

if (even_count % 2 == 0)

even_count++;

std::cout << "Even Digits Program\n";

std::cout << "Enter a number greater than 0.\n";

std::cin >> n;

std::cout << "This number has " << even_count << " even digit(s).";

The problem I am having is that it keeps returning 1 even digit for any number I input.

If you could help me with this I would greatly appreciate it.

Answer :

Explanation:

Hey there! you need not to panic about it ,your program didn't have Driver program i.e main program! the correct & working code is given below:

// C++ program to count  even digits  in a given number .

#include <iostream>  

using namespace std;  

// Function to count digits

int countEven(int n)  

{  

int even_count = 0;    

while (n > 0)  

{  

 int rem = n % 10;  

 if (rem % 2 == 0)  

  even_count++;  

 n = n / 10;  

}  

cout << "Even count : "

 << even_count;  

if (even_count % 2 == 0 )  

 return 1;  

else

 return 0;  

}  

// Driver Code  

int main()  

{  

int n;  

std::cin >>n;

int t = countEven(n);  

return 0;  

}  

Other Questions