我应该在这里取代什么来使我的运动起作用?

时间:2015-11-26 22:30:08

标签: c++

我想将数字中的偶数乘以,例如,如果我输入22,我希望程序乘以2 * 2。我应该在我的计划中替换什么来完成我的目标?

#include <iostream>

using namespace std;

int main()
{
    int n;
    int result;
    cout << "Enter Number bigger then 9" << endl;
    cin>>n;
    if(n<9)
{
    cout<< "You entered a number smaller then 9" << endl;
}
else
{
    cout << "You Entered: " <<n<<endl;
    while (n >= 100)
    {
        n /= 10;
        return n % 10;

    }

    if(n % 2 == 0)
    {
        result = n*n;
        cout << "The Result from multiple digits is : "<<result<<endl;
    }
    else
    {
           cout << "The Digit is not even"<< endl;

    }
}

1 个答案:

答案 0 :(得分:0)

逻辑是错误的。 你需要提取每个数字,找出它是否可被2整除并相乘。 这是一种方法:

#include <iostream>
#include <cmath>


using namespace std;

int main()
{
    int n;
    int result = 1;
    cout << "Enter Number bigger then 9" << endl;
    cin>>n;
    if(n<9)
    {
        cout<< "You entered a number smaller then 9" << endl;
    }
    else
    {
        cout << "You Entered: " <<n<<endl;
        int i = 10;
        while (n > 0)
        {
            int remainder = n % i;
            cout << "remainder: " << remainder <<endl;
            if ((remainder % 2) == 0)
            {
                result *= remainder;
            }
            n = n/10;
        }

        cout << "result: " << result << endl;
    }

    return 0;
}