[英]C++ How to count the number of even digits in a user entered number?
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.该程序是一个基于菜单的程序,其中这是选项 1。我认为到目前为止我的其余代码运行良好,这只是我被挂断的这部分的数学。 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!谢谢!
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.正如 Sam Varshavchik 所建议的那样,描述问题和解决方案,看看你在代码中做了什么以及它们如何匹配。
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以数字123456为例
How to get to 12345 will be something I'll let you figure out.我会让你弄清楚如何到达12345。
Then you will be missing how to define the end of the loop.那么你将错过如何定义循环的结束。
Cheers!干杯!
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;
}
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.只需简单地执行此操作即可在数字中查找偶数位 1. 必须在循环中编写查找偶数位的代码 2. 每次迭代后减少数字以移动下一位。 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.