Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Simple if Statement Example - Odd / Even Numbers

% cat even.cpp
// check for odd or even
#include<iostream>
using namespace std;

int main()
{
  int num;
  cout << "Enter an integer: ";

  cin >> num;

  if( num%2 == 0)
    cout << num << " is an even number" << endl;

  if( num%2 != 0)
    cout << num << " is an odd number" << endl;

  return 0;

}

% g++ even.cpp
% a.out
Enter an integer: 16
16 is an even number
% a.out
Enter an integer: 19
19 is an odd number

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming