Read a number from the keyboard and reports whether it is odd or even. - C++ File Stream

C++ examples for File Stream:cin

Description

Read a number from the keyboard and reports whether it is odd or even.

Demo Code

#include <iostream>
using namespace std;
int main()//from  ww w .  jav  a 2  s  .c o  m
{
   int  n = 0, remainder = 0;
   // Get a number from the keyboard.
   cout << "Enter a number and press ENTER: ";
   cin >> n;
   // Get remainder after dividing by 2.
   remainder = n % 2;
   // If remainder is 0, the number input is even.
   if (remainder == 0) {
      cout << "The number is even." << endl;
   } else {
      cout << "The number is odd." << endl;
   }
   return 0;
}

Result


Related Tutorials