Thursday, December 13, 2012

C++ TUTORIAL FOR BEGINNERS: How to determine a given number if it is Odd or Even number


/*
PROGRAM: How to determine a given number if it is Odd or Even number

PURPOSE:
         In order to know whether a number is even or odd, we must use the concept
of the Modulo Operation (mod). The modulo operation will compute the Remainder of
dividing a given number by another number. For example, if we divide 10 by 2, the
quotient or result is 5 and the remainder is 0. Therefore 10 mod 2 is 0.
         On the other hand, if we divide 11 by 2, the quotient or result is 5
Remainder 1. In this case 11 mod 2 is 1.
         In our C++ program, we will use the Integer modulo operator % to determine
if a number input by a user is an odd or even number.

LANGUAGE: C++
AUTHOR: eternaltreasures
DATE: 2010 October 6
*/

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
      
void modulo_odd_even ()
{
int input_number;

cout << endl;
cout << "Determination of Odd or Even Numbers" << endl << endl;
cout << "Please enter any number: "; cin >> input_number;

//input_nmuber % 2 will find the remainder of input_number divided by 2.
//If the remainder is 0, then the number is even, otherwise it is odd.

    if (input_number % 2 == 0)
        cout << input_number << " is an Even Number" << endl << endl;
    else
        cout << input_number << " is an Odd Number" << endl << endl;   
   
}
      
      
int main()
{

bool endless_loop = true;

//for loop code leading to an ENDLESS LOOP (online mode)
//for loop without increment

for (endless_loop; (endless_loop != false);)
    modulo_odd_even ();
}

No comments:

Post a Comment