C++ Example – Program to Check Whether Number is Even or Odd




cpp tutorials
cpp tutorials

Write a C++ Program to find if the number is Odd or Even.
Basically Even numbers are those which are divisible by 2, and Odd number are those which numbers are not divisible 2.

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int no;
  cout << "Enter any number: ";
  cin >> no;
  if(no%2==0)
  {
    cout<<"Even number";
  }
  else
  {
    cout<<"Odd number";
  }
  return 0;
}

/*
Enter any num: 55
Odd num
*/

 

C++ code to Check if the given number is Even or Odd Using ternary or conditional Operator

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int number;
  cout << "Enter any number : ";
  cin >> number;
  (number % 2 == 0) ? cout<<"Even number" : cout<<"Odd number";
  return 0;
}

/*
OUTPUT:
Enter any number : 77
Odd number
*/

C++  program to check if the given number is Even or Odd Using Bitwise Operator

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int number;
  cout<<"Enter any number: ";
  cin>>number;
  if(number & 1)
  {
    cout<<number<<" Odd number";
  }
  else
  {
    cout<< number <<" Even number";
  }
  return 0;
}

/*
OUTPUT:
Enter any number: 33
33 Odd number
*/

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*