Reputation: 37
In this problem I'm trying to find the number of even digits in a user input number. I am unable to use arrays. The program is a menu based program where this is option 1. I think I have the rest of the code working well so far, it is just the math for this part that I am getting hung up on. Any help would be greatly appreciated.
int main() {
int choice; // user input for menu options
const int evenDig = 1, // constants for menu choices
fact = 2,
quit = 3;
cout << "Welcome to playing with numbers!\n";
do
{
showMenu (); // displays menu
cin >> choice;
// call getValidUserInputPosNumGT0, passing the value in choice
// as an argument
getValidUserInputPosNumGT0(choice);
if (choice == evenDig) // option 1
{
int num; // user entered number
cout << "Enter a positive number greater than zero...";
cin >> num;
numEvenDigits(num);
}
}while(choice != quit);
return 0;
}
void showMenu ()
{
cout << "1) Count the even digits in a number\n"
<< "2) Compute the factorial of a number\n"
<< "3) Quit\n"
<< "Select an option (1..3).. ";
}
void getValidUserInputPosNumGT0 (int choice) // validation function
{
while ( choice < 1 || choice > 3) // input validation loop
{
cout << "Select an option (1..3).\n";
cin >> choice;
}
}
int numEvenDigits (int num)
{
int even = 0; // int that will be returned as number of even numbers
if ( num > 0)
{
int rem = num % 10;
if (rem % 2 == 0)
even++;
cout << "numEvenDigits("<<num<<") = "<< even;
cout << endl;
}
else
cout << "Please enter a positive nonzero number." << endl;
}
Thank you!
Upvotes: 0
Views: 674
Reputation: 77
Just simply do this to find even digits in a number 1.Your code to find even digits must be written in a loop 2. Decrease number after each iteration to move the next digit. Remember we count the digits from right to left.
Int number, x, even=0;
Cin>> number;
While(num>=0)
{
X=num%10;
If(x%2==0)
Even++;
Num=num/10;// decrease number
}
Upvotes: 0
Reputation: 4711
Since there are multiple ways to solve what you are trying to acomplish, instead of providing a solution, I will try to give a couple hints on the math and thought process to help you figure out your own solution.
As Sam Varshavchik suggests, describe the problem and the solution and see what you have done in code and how it matches up.
So you have sucessfully done this in your code once
int rem = num % 10;
if (rem % 2 == 0)
even++;
What about the rest of the numbers? --> you are missing a loop somewhere
Take for exmple the number 123456
How to get to 12345 will be something I'll let you figure out.
Then you will be missing how to define the end of the loop.
Cheers!
Upvotes: 1
Reputation: 2399
You Need to change some of your code
void getValidUserInputPosNumGT0 (int choice);
To
void getValidUserInputPosNumGT0 (int &choice);
Because If you want to modify choice you must pass it by reference not by value.
And
int numEvenDigits (int num)
{
int even = 0; // int that will be returned as number of even numbers
if ( num > 0)
{
int rem = num % 10;
if (rem % 2 == 0)
even++;
cout << "numEvenDigits("<<num<<") = "<< even;
cout << endl;
}
else
cout << "Please enter a positive nonzero number." << endl;
}
To
int numEvenDigits (int num)
{
int even = 0;
if ( num > 0)
{
for (int val =num;val>0;val/=10)
{
if (val % 2 == 0)
even++;
}
cout << "numEvenDigits("<<num<<") = "<< even;
cout << endl;
}
else
cout << "Please enter a positive nonzero number." << endl;
return even;
}
Or, This is similar to your code
int numEvenDigits (int num)
{
int even = 0,val=num,rem;
if ( num > 0)
{
while(val>0)
{
rem=val%10;
if(rem%2==0)
even++;
val/=10;
}
cout << "numEvenDigits("<<num<<") = "<< even;
cout << endl;
}
else
cout << "Please enter a positive nonzero number." << endl;
return even;
}
Upvotes: 0