8.c++ program to check whether the number is even or odd



FIRST METHOD:

 

// C++ program to check whether the number is even or odd using if else


INPUT:

#include<iostream>

using namespace std;

int main()

{

    int z;

    cout<<"\n Enter an integer:";

    cin>>z;

    if(z%2==0)

        cout<<"\n\t"<<z<<" is Even "<<endl;

    else

        cout<<"\n\t"<<z<<" is Odd "<<endl;

    return 0;


}


SECOND METHOD:


// C++ program to check whether the number is even or odd using ternary operators


INPUT:


#include<iostream>

using namespace std;

int main()

{

    int z;

    cout<<"Enter an integer : ";

    cin>>z;

    (z%2==0) ? cout<<"\n\t"<<z<<" is Even "<<endl : cout<<"\n\t"<<z<<" is Odd "<<endl;


    return 0;


}


OUTPUT:





Comments