2016-03-07 32 views
-1

我想創建一個程序來完成標題所說的內容,通過5整除意味着整數值。這裏是迄今爲止,雖然它是不完整的代碼: -創建一個程序來告訴用戶一個數字是否可以被整除5

#include <iostream> 
using namespace std; 

int main() 

{ 

    int x; 
    cout << "Enter a number to see if its divisable by 5" << endl; 
    cin >> x; 
    x = x/5; 
    if (x ==) 
    { 
     cout << "yes it is divisible by 5 and the value is" << x << endl; 
    } 
    else 
    { 
     cout << "no its not divisable by 5" << endl; 
    } 

    return 0; 
} 

我堅持以放什麼的if語句,使其認識到,如果分配給X的數量是5或不能整除。有人可以幫助我嗎?

回答

5

你想這(使用modulo%操盤手)

cin >> x; 
//x = x/5; remove this 
if (x % 5 == 0) 
{ 
    cout << "yes it is divisible by 5 and the value is" << x/5 << endl; 
} 
else 
{ 
    cout << "no its not divisable by 5" << endl; 
} 
+0

我會試試這個,但是x/5不一定等於0,它必須等於任何整數。你的代碼能適應這個嗎? –

+0

@FlewittConnor是的,檢查更新 –

+0

謝謝你這個作品。所以模數(%)是餘數? –

1

使用模%而不是/x = x/5;

模給出提示25%5 = 0,其中25/5 = 5

if (x ==)聲明把x==0

+0

謝謝我已經得到它的工作。你能解釋一下25%5 = 0是什麼意思嗎?餘數是模數? –

+0

是modulo%提醒除法,'/'給出除法 – masternone

+0

的商我看,謝謝 –

相關問題