-1
int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    cout << "you entered ";
    cout << num;
    cout << endl;
    return 0;
}

How will a program know when an odd number is input by the user?

For an "If statement"... I can't say If (num == odd) { cout << "all done"

timwheelz
  • 25
  • 1
  • 4
  • Possible duplicate of [How do I check if an integer is even or odd?](http://stackoverflow.com/questions/160930/how-do-i-check-if-an-integer-is-even-or-odd) – mech Feb 22 '16 at 05:31
  • You'll need to look at loops. Do-while loop is probably what you're looking for. Do "prompt" while "num % 2 != 0" – M.Alnashmi Feb 22 '16 at 05:34

1 Answers1

1

By using the Modulus Operator, you can detect whether a number is even or odd. You can think of the modulus operator (%) as giving you the 'remainder' after a division. For example, 12 % 2 would give you 0. 13 % 2 would give you 1. In this way, just do

if (variable % 2 == 1)

to check if your variable is odd.

Ethan Fischer
  • 1,229
  • 2
  • 18
  • 30