2015-11-26 18 views
-4

我想乘以一個數字的偶數位,例如,如果我輸入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

我建議'return'語句。它會結束你的程序。 –

+1

您可能想將'if'語句移到'while'循環中。 –

+0

代碼的當前行爲是什麼?你有沒有輸入和它的行爲,以及你的預期行爲的例子?如果使用61或66,99,輸出是多少? – Tas

回答

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; 
} 
+0

非常感謝! – Frosten

相關問題