Wednesday, March 7, 2012

Odd or Even Number Checker (C++)

This is just a basic program (in C++) to check if the user had entered an odd or even number.


#include "iostream"

int EvenOdd(int num1);//declears the function
int EvOd, ipNum, x;
char ipChar;

void main()
{
    //extra coding, enhanced with "do...while(condition)" loop
    do
    {
        std::cout<<"=============================================================="<<std::endl;
        std::cout<<"Please enter an integer ";
        std::cin>>ipNum;//gets the input integer
       
        int y = EvenOdd(EvOd);
        if (y == 1){
            std::cout<<ipNum<<" is an even number"<<std::endl;
        }
        else {
            std::cout<<ipNum<<" is an odd number"<<std::endl;
        }

        std::cout<<"Enter 'N' if you wish to stop "; //ask user if he wants to continue
        std::cout<<"\n\t 'Y' if wish to continue ";
        std::cin>>ipChar;

        std::cout<<"______________________________________________________________\n\n"<<std::endl;

            if ((ipChar == 'Y') || (ipChar == 'y'))//loop control
                x = 1; //continues the loop (1 means true)
            else
                x = 0; //stops the the loop (0 means false)
           
    }while (x !=0);

    system("PAUSE"); //holds command window from crashing
}


int EvenOdd(int Num1)//checking function
{
    double Result = ipNum % 2;
    if (Result==0)//tests for even number
        return (1); //returns a 1 for even number input
    else if (Result !=0)
        return (0); //returns a 0 as odd number input
}

No comments:

Post a Comment